双向循环链表-C语言版

来源:互联网 发布:php需要编译吗 编辑:程序博客网 时间:2024/05/07 16:52
源文件部分:#include<stdio.h>#include<string.h>#include<malloc.h>typedef int Elemtype;#include"Delist.h"int main(){Dlnode head=NULL;instruction(head);return 0;}头文件部分:typedef struct DLnode        {Elemtype data;struct DLnode *prior;    //节点的声明定义struct DLnode *next;}DLnode,*Dlnode;void Init_Dlist(Dlnode &head)    //循环双向初始化 {head=(Dlnode)malloc(sizeof(DLnode));head->prior=head;head->next=head;}int Empty_Dlist(Dlnode head)       //双向判空{if(head->prior==head&&head->next==head)return 1;return 0;}void Insert_Dlist(DLnode *head,Elemtype e)   //头插法-双向(新增){Dlnode p=NULL;p=(DLnode *)malloc(sizeof(DLnode));            //因为是双向循环链表,所以不存在双向的那个问题if(!p){printf("对不起,已无更多的内存单元得到分配!!!\n");return ;}p->data=e;p->next=head->next;p->next->prior=p;p->prior=head;//printf("*%d*\n",head->next);head->next=p;}int Length_Dlist(Dlnode head){DLnode *p=NULL;int len=0;if(Empty_Dlist(head))return 0;p=head->next;while(p!=head){len++;p=p->next;}return len;}int Delete_Dlist(DLnode *head,Elemtype where)        //按位置删除{DLnode *p=NULL;int i=1;p=head->next;if(Empty_Dlist(head)){printf("对不起,链表是空的,无法完成删除操作!!!\n");return 0;}if(where>Length_Dlist(head)||where<0){printf("对不起,你删除的位置是不合法的,请重新输入!!!\n");return 0;}while(i<where){p=p->next;i++;}p->prior->next=p->prior->next->next;p->prior->next->prior=p->prior;printf("删除成功!!!\n");return p->data;}void Insearch_Dlist(Dlnode head,Elemtype e)      //按元素查找  {DLnode *p=NULL;int len=1;if(Empty_Dlist(head)){printf("对不起,链表是空的,无法完成查找操作!!!\n");return ;}p=head->next;while(p!=head){if(p->data==e){printf("你要查找的元素位于链表的第%d个位置上.\n",len);return ;}p=p->next;len++;}printf("对不起,你要查找的元素不存在,请重新输入!!!\n");return ;}void Modify_Dlist(DLnode *head,Elemtype where,Elemtype e)    //按位置修改{DLnode *p=NULL;int len=1;p=head->next;while(len<where){p=p->next;len++;}p->data=e;printf("修改成功!\n");return ;}void Print_Dlist(Dlnode head)          //打印操作{Dlnode p=head->next;if(Empty_Dlist(head)){printf("对不起,链表是空的,无法完成打印操作!!!\n");return ;}while(head!=p){printf("%d ",p->data);p=p->next;}printf("\n");return ;}void Destory_Dlist(Dlnode head)            //销毁清空操作{Dlnode p=head->next;while(p!=head){p->prior->next=p->next;p->next->prior=p->prior;p=head->next;}printf("销毁成功!\n");}void instruction(Dlnode head)             //功能函数 {int n,m,t,a,b,len1,index;printf("\t\t1、初始操作\n");printf("\t\t2、新增操作\n");              //为什么不能在这里定义head指针---因为每次调用功能函数后,head指针又被重新初始化了    printf("\t\t3、删除操作\n");printf("\t\t4、查找操作\n");printf("\t\t5、修改操作\n");printf("\t\t6、销毁操作\n");printf("\t\t7、求长操作\n");printf("\t\t8、打印操作\n");printf("\t\t9、退出程序\n");printf("请输入你所需要完成的指令:\n");do{scanf("%d",&n);if(n<1||n>9)printf("对不起,你输入的指令编号是无效的,请重新输入!!!\n");}while(n<1||n>9);switch(n){case 1:Init_Dlist(head);            //初始化操作printf("已完成双向链表初始化,请输入你要添加的元素个数!\n");scanf("%d",&n);while(n--){int x;scanf("%d",&x);Insert_Dlist(head,x);}printf("完成建表操作!\n");break;case 2:                                  //新增操作if(!head){printf("对不起,请先完成初始化操作再做该选择!!!\n");break;}printf("请输入你要添加的元素个数!\n");scanf("%d",&n);while(n--){int x;scanf("%d",&x);Insert_Dlist(head,x);}printf("增添成功!\n");break;case 3:printf("请输入你所要删除的节点的位置:\n");scanf("%d",&n);Delete_Dlist(head,n);                             //删除操作break;case 4:printf("请输入你所要查找的元素:\n");scanf("%d",&m);Insearch_Dlist(head,m);                       //查找操作 break;case 5:if(Empty_Dlist(head)){printf("对不起,链表是空的,无法完成修改操作!!!\n");break ;}printf("请输入你要更改的元素队列位置:\n");          //修改操作 do{scanf("%d",&a);if(a<1||a>Length_Dlist(head))printf("对不起,你所输入的元素位置不在区域内,请重新输入!!!\n");}while(a<1||a>Length_Dlist(head));printf("请输入修改后的值:\n");scanf("%d",&b);Modify_Dlist(head,a,b);break;case 6:Destory_Dlist(head);            //销毁操作 break;case 7:len1=Length_Dlist(head);             //返回链长操作 printf("当前链队列的长度为:%d\n",len1);break;case 8:Print_Dlist(head);        //打印操作 break;case 9:                   //退出操作 return;default:instruction(head);break;}instruction(head);}

1 0
原创粉丝点击