c语言:双向循环链表的实现

// 双向循环链表实现.cpp : 定义控制台应用程序的入口点。

#include<iostream>
#include<stdio.h>
#include<math.h>
#define LEN  sizeof(struct DSCriculist)
using namespace std;
typedef struct DSCriculist  //定义结构体
{
	int data;                 //数据域
	struct DSCriculist *prior;//前向指针域
	struct DSCriculist *next; //后向指针域
}DSCriculist;
//函数声明部分
void Error(char* s);//错误处理函数
DSCriculist *Creat_DSCriculist();  //创建双向循环链表
DSCriculist *Find_DSCriculist(DSCriculist & L, int i);//查找双向循环链表中第i个结点
void Insert_DSCriculist(DSCriculist & L, int i, int e);//向双向循环链表中位置i处插入数据域为e的结点
void Delete_DSCriculist(DSCriculist & L, int i, int e);//删除双向循环链表中第i个结点,并用e返回该结点数据域的值
void Search_DSCriculist(DSCriculist &L, int e);//查找双向循环链表中数据域为e的结点,并输出
int Getlength_DSCriculist(DSCriculist & L);//计算上相循环链表的长度
void Print_DSCriculist(DSCriculist &L);//以此输出双向循环链表的数据域的值
//函数定义部分
void Error(char *s) //错误处理函数
{
	cout << s << endl;
	exit(1);
}

DSCriculist *Creat_DSCriculist()
{
	DSCriculist *head, *tail, *ptr;
	int length = 0;
	int number = 0;
	head = (struct DSCriculist*)malloc(LEN);
	if (NULL == head)
		Error("内存分配失败!");
	head->data = 0;
	head->next = head;
	head->prior = head;
	tail = head;
	cout << "请输入链表长度:";
	cin >> length;
	cout << "请输入链表数据:" << endl;
	for (int i = 0; i < length; i++)
	{
		ptr = (struct DSCriculist*)malloc(LEN);
		if (NULL == ptr)
			Error("内存分配失败!");
		cin >> number;
		ptr->data = number;
		tail->next = ptr;
		ptr->prior = tail;
		ptr->next = head;
		head->prior = ptr;
		tail = ptr;
	}
	cout << "双向链表创建成功!" << endl;
	return head;
}

DSCriculist *Find_DSCriculist(DSCriculist & L, int i)
{
	DSCriculist *prev = L.next;
	int j = 1;
	while ((prev != &L) && (j < i))
	{
		prev = prev->next;
		j++;
	}
	return prev;
}

void Insert_DSCriculist(DSCriculist & L, int i, int e)
{
	DSCriculist *prev = Find_DSCriculist(L, i);
	DSCriculist *ptr = (struct DSCriculist*)malloc(LEN);
	if (NULL == ptr)
		Error("内存分配失败!");
	ptr->data = e;
	prev->prior->next = ptr;
	ptr->next = prev;
	prev->prior = ptr;
	ptr->prior = prev->prior;
}
void Delete_DSCriculist(DSCriculist & L, int i, int e)//删除双向循环链表中第i个结点,并用e返回该结点数据域的值
{
	DSCriculist *prev = Find_DSCriculist(L, i);
	e = prev->data;
	if (prev->next == &L)
	{
		DSCriculist *pt = prev->prior;
		pt->next = &L;
	}
	else
	{
		prev->prior->next = prev->next;
		prev->next->prior = prev->prior;
	}
	cout << "删除的元素是:" << e << endl;;
	delete prev;
}
void Search_DSCriculist(DSCriculist &L, int e)//查找双向循环链表中数据域为e的结点,并输出该节点的位置
{
	DSCriculist *prev = L.next;
	if (prev == &L)
		Error("位置错误!");
	int k = 1;
	while ((prev->next != &L) && (prev->data != e))
	{
		prev = prev->next;
		k++;
	}
	cout << "该元素在链表中的位置是:" << k << endl;
}

int Getlength_DSCriculist(DSCriculist & L)
{
	DSCriculist *prev = &L;
	int length = 0;
	while (prev->next != &L)
	{
		prev = prev->next;
		length++;
	}
	return length;
}
void Print_DSCriculist(DSCriculist &L)
{
	DSCriculist *prev = L.next;
	if (prev == &L)
		Error("该链表为空表!");
	cout << "该链表数据是:" << endl;
	while (prev->next!= &L)
	{
		cout << prev->data << " ";
		prev = prev->next;
	}
	cout << endl;
}

int main()
{
	DSCriculist *p = NULL;
	int m, n, t, q;
	int e = 0;
	p = Creat_DSCriculist();
	Print_DSCriculist(*p);
	cout << "该链表的长度是:" << Getlength_DSCriculist(*p) << endl;
	cout << "请输入你要插入的位置:";
	cin >> m;
	cout << "请输入你要插入的元素:";
	cin >> n;
	Insert_DSCriculist(*p, m, n);
	Print_DSCriculist(*p);
	cout << "插入后的链表的长度是:" << Getlength_DSCriculist(*p) << endl;
	
	cout << "请输入你要删除的位置:";
	cin >> q;
	Delete_DSCriculist(*p, q, e);
	Print_DSCriculist(*p);
	cout << "删除后的链表的长度是:" << Getlength_DSCriculist(*p) << endl;
	cout << "请输入要查找的元素:";
	cin >> t;
	Search_DSCriculist(*p, t);
	return 0;
}

你可能感兴趣的:(数据结构,编程语言,链表,C语言)