//函数声明:#include"cirtwowaylinklist.h"#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>#include<stdlib.h>typedefintElemType;typedefstructCirDulNode{structCirDulNode*prior;ElemTypedata;structCirDulNode*next;}DulNode;voidjudgement_NULL(DulNode*head);DulNode*creat();//创建链表voidinsert(DulNode*head,inti,ElemTypex);//插入结点voiddelete_element(DulNode*head,ElemTypex);//删除链表中所有值为X的结点voiddelete_place(DulNode*head,inti);//删除链表中第i个结点voidfind_place(DulNode*head,ElemTypex);//寻找链表中所有x,所在的结点位置voidfind_element(DulNode*head,inti);//寻找第i个结点上的值voidlength(DulNode*head);voidinitslinklist(DulNode*head);//释放整个链表voidsortrank(DulNode*head);//对链表进行排序voidoutput_order(DulNode*head);//正序打印voidoutput_back(DulNode*head);//逆序打印intmeans();//选择方式//函数实现:#include"cirtwowaylinklist.c"#include"cirtwowaylinklist.h"voidjudgement_NULL(DulNode*head)//判断动态内存是否开辟成功{if(head==NULL){perror("outofmemory\n");exit(EXIT_FAILURE);}}DulNode*creat(){DulNode*head,*p,*r;ElemTypex;head=(DulNode*)malloc(sizeof(DulNode));judgement_NULL(head);r=head;printf("开始建立:");while(1){scanf("%d",&x);if(x!=0)//建立链表,以0作为结束标志{p=(DulNode*)malloc(sizeof(DulNode));judgement_NULL(p);p->data=x;r->next=p;p->prior=r;r=p;}elsebreak;}head->prior=r;//将链表首尾链接r->next=head;printf("创建成功\n");returnhead;}voidinsert(DulNode*head,inti,ElemTypex)//在i位置上插入一个X,因为链表循环,所以i>1就一定能找到一个位置{DulNode*p,*r;p=(DulNode*)malloc(sizeof(DulNode));judgement_NULL(p);p->data=x;r=head;while(1){if(i<=1)break;r=r->next;i--;}if(i<1)printf("没有该结点\n");else{p->next=r->next;//在头结点之前,之后都可以插入p->prior=r;r->next=p;p->next->prior=p;}}voiddelete_element(DulNode*head,ElemTypex)//删除链表中所有的x元素所在的结点{DulNode*p,*r;intcount=0;p=head->next;while(p!=head){if(p->data==x){count++;r=p;p=p->next;r->prior->next=r->next;r->next->prior=r->prior;free(r);r=NULL;}elsep=p->next;}if(count==0)printf("链表中没有此元素\n");elseprintf("共删除%d个\n",count);}voiddelete_place(DulNode*head,inti)//删除一个指定位置的结点,结点位置i>0{DulNode*p;p=head;while(1){if(i<=1)break;p=p->next;i--;}if(i<1)printf("没有该结点\n");else{if(p->next=head)//如果要删除的结点是头结点则跳过删除头结点之后的结点{p=head->next;head->next=p->next;p->next->prior=head;}else{p->prior->next=p->next;p->next->prior=p->prior;free(p);}}}voidfind_place(DulNode*head,ElemTypex)//找到链表中所有的x元素所在的结点位置{intcount=0;intflag=0;DulNode*p;p=head->next;while(p!=head){count++;if(p->data==x){flag++;printf("此元素结点位置:%d\n",count);}p=p->next;}if(flag==0)printf("链表中没有此元素\n");}voidfind_element(DulNode*head,inti)//找到i结点上的元素,并输出{DulNode*p;p=head->next;while(p!=head){if(i<=1)break;p=p->next;i--;}if(p==head||i!=1)printf("没有该结点\n");elseprintf("此结点元素:%d\n",p->data);}voidlength(DulNode*head){intlen=0;DulNode*p;p=head->next;while(p!=head){len++;p=p->next;}printf("链表长度:%d\n",len);}voidinitslinklist(DulNode*head)//释放整个链表{DulNode*p;p=head->next;while(p!=head){p=p->next;free(p->prior);p->prior=NULL;}free(head);head=NULL;p=NULL;}voidsortrank(DulNode*head)//对整个链表进行排序{DulNode*p,*r;ElemTypetmp;p=head->next;while(p->next!=head){r=head->next;while(r->next!=head){if((r->data)>(r->next->data)){tmp=r->data;r->data=r->next->data;r->next->data=tmp;}r=r->next;}p=p->next;}printf("排序成功\n");}voidoutput_order(DulNode*head)//正向打印整个链表{DulNode*p;p=head;if(head->next==head)printf("链表为空\n");else{printf("正序打印链表:");p=p->next;while(p!=head){printf("%d",p->data);p=p->next;}printf("\n");}}voidoutput_back(DulNode*head)//反向打印整个链表{DulNode*p;p=head->prior;if(head->next==head)printf("链表为空\n");else{printf("反向打印链表:");while(p!=head){printf("%d",p->data);p=p->prior;}printf("\n");}}intmeans()//选择以哪种方式进行操作{intm=0;while(1){printf("请选择方式:");scanf("%d",&m);if(m==1||m==2)break;printf("选择无效,请重新选择\n");}returnm;}//函数测试:#include"cirtwowaylinklist.h"intmain(){DulNode*ret=NULL;intn=0;inti=0;ElemTypex;printf("*********************************************\n");printf("*********************************************\n");printf("*1.CreatLinkList2.Insert**********\n");printf("*3.Delete4.Find**********\n");printf("*5.Length6.Output**********\n");printf("*7.InitsLinkLinst8.Sortrank********\n");printf("*0.Exit*******************\n\n\n");while(1){printf("请选择功能:");scanf("%d",&n);if(n==0)//选择0直接退出{free(ret);//退出前先释放列表exit(1);}if(ret==NULL)//如果ret为空,则首先建立链表{if(n==1){printf("创建链表以0作为结束标志\n");ret=creat();}elseprintf("请先建立链表\n");}else{switch(n)//选择剩下的功能{case1://当ret不为空时不能建立链表printf("当前链表未结束,请先初始化链表\n");break;case2:printf("请输入要插入的元素和位置:");scanf("%d",&x);scanf("%d",&i);insert(ret,i,x);break;case3:printf("*1.delete_element2.delete_place*\n");if(means()==1){printf("请输入要删除的元素:");scanf("%d",&x);delete_element(ret,x);}else{printf("请输入要删除的结点:");scanf("%d",&i);delete_place(ret,i);}break;case4:printf("*1.find_place2.find_element*\n");if(means()==1){printf("请输入要查找的元素:");scanf("%d",&x);find_place(ret,x);}else{printf("请输入要查找的位置:");scanf("%d",&i);find_element(ret,i);}break;case5:length(ret);break;case6:printf("*1.output_order2.output_back*\n");if(means()==1)output_order(ret);elseoutput_back(ret);break;case7://将当前链表释放initslinklist(ret);ret=NULL;break;case8:sortrank(ret);break;default:printf("选择无效,请重新选择\n");break;}//switch()语句结束}n=0;}//循环结束system("pause");return0;}