雙向迴圈鏈表-C語言版,迴圈-c語言版

來源:互聯網
上載者:User

雙向迴圈鏈表-C語言版,迴圈-c語言版

源檔案部分:#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);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.