数据结构(单链表的相关操作)
生活随笔
收集整理的這篇文章主要介紹了
数据结构(单链表的相关操作)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
#include?<stdio.h> #include?<stdlib.h> #define?ElemType?int #define?Status?int #define?OK?1 #define?ERROR?0typedef?struct?List {ElemType?data;struct?List?*next; }linklist;//定義結構類型Status?CreateList(linklist?*L) {printf("\nPlease?input?the?linklist?end?with?0:\n");linklist?*p,*r;L->next=NULL;r=L;p=(linklist?*)malloc(sizeof(linklist));if(!p)return?ERROR;scanf("%d",&p->data);while(p->data!=0){p->next=NULL;r->next=p;r=p;p=(linklist?*)malloc(sizeof(linklist));scanf("%d",&p->data);}return?OK; }//尾插法創建一個升序單鏈表Status?display(linklist?*L) {linklist?*p;p=L->next;printf("%d",p->data);p=p->next;while(p){printf("->%d",p->data);p=p->next;}printf("\n");return?OK; }//顯示單鏈表Status?reverse(linklist?*L) {linklist?*p,*q,*u;p=L->next;if(p==NULL)return?ERROR;q=L->next->next;p->next=NULL;while(q){u=q->next;p->next=u;q->next=L->next;L->next=q;q=u;}return?OK; }//單鏈表逆置Status?listinsert(linklist?*L,?ElemType?e) {linklist?*pre,*p,*s;pre=L;p=L->next;while(p&&p->data<e){pre=p;p=p->next;}s=(linklist?*)malloc(sizeof(linklist));s->data=e;s->next=p;pre->next=s;return?OK; }//單鏈表中插入一個元素之后仍然有序linklist?*MergeList(linklist?*La,linklist?*Lb?) {linklist?*Lc;linklist?*pa,*pb,*pc;pa=La->next;pb=Lb->next;Lc=pc=La;while(pa&&pb){if(pa->data<=pb->data){pc->next=pa;pc=pa;pa=pa->next;}else{pc->next=pb;pc=pb;pb=pb->next;}}pc->next=pa?pa:pb;return?Lc;//返回合并后鏈表的頭指針 }//合并單鏈表void?listdel(linklist?*L,ElemType?e) {linklist?*p,*q;p=L->next;q=L;while(p){if(e==p->data){q->next=p->next;p=p->next;}else{p=p->next;q=q->next;}} }//單鏈表的刪除操作void?delete_A(linklist?*B,linklist?*C) {linklist?*A=(linklist?*)malloc(sizeof(linklist));printf("\nCreate?the?increasing?list?A:\n");CreateList(A);ElemType?del[100];int?i=0,j;linklist?*p,*q;p=B->next;q=C->next;while(p&&q){if(p->data!=q->data)p=p->next;else{del[i]=p->data;p=p->next;q=q->next;i++;}}j=i;for(i=0;i<=j;i++){listdel(A,del[i]);}printf("\nThe?new?A?is:\n");display(A); }//刪除A中B&C相同的元素int?main(void) {int?choice;ElemType?e;linklist?*L=(linklist?*)malloc(sizeof(linklist));linklist?*La=(linklist?*)malloc(sizeof(linklist));linklist?*Lb=(linklist?*)malloc(sizeof(linklist));linklist?*Lc;linklist?*B=(linklist?*)malloc(sizeof(linklist));linklist?*C=(linklist?*)malloc(sizeof(linklist));do{printf("\n********************************\n");printf("1.Create?a?linklist?orderly\n");printf("2.Display?the?linklist\n");printf("3.Insert?a?element\n");printf("4.Delet?a?element?from?L\n");printf("5.Reverse?the?linklist\n");printf("6.Merge&Reverse?two?linklist\n");printf("7.Delet?the?same?element?which?appears?in?B&C?from?A?\n");printf("0.Exit\n");printf("\n\n?Press?a?key?for?your?choice:\n");scanf("%d",&choice);switch(choice){case?1:CreateList(L);printf("\nThe?linklist?L?has?been?created?successfully!\n");getchar();getchar();break;case?2:printf("\nThe?linklist?L?is:\n");display(L);getchar();getchar();break;case?3:printf("\nPlease?input?the?element?you?want?to?insert:\n");scanf("%d",&e);listinsert(L,e);display(L);getchar();getchar();break;case?4:printf("\nPlease?input?the?element?you?want?to?delete:\n");scanf("%d",&e);listdel(L,e);printf("\nThe?list?after?delet?is?:\n");display(L);getchar();getchar();break;case?5:reverse(L);printf("\nThe?list?after?reverse?is?:\n");display(L);getchar();getchar();break;case?6:printf("\nCreate?the?list?la:\n");CreateList(La);printf("\nCreate?the?list?lb:\n");CreateList(Lb);printf("The?list?after?merge?is?:\n");Lc=MergeList(La,Lb);reverse(Lc);display(Lc);getchar();getchar();break;case?7:printf("\nCreate?the?increasing?list?B:\n");CreateList(B);printf("\nCreate?the?increasing?list?C:\n");CreateList(C);delete_A(B,C);getchar();getchar();break;}}while(choice!=0);}1:關于case7:
輸入不同數據結果不太一樣,有時候對有時候不對,debug也沒有查出來為什么。
求解答啊啊 啊啊啊。
轉載于:https://my.oschina.net/hfancy/blog/645729
總結
以上是生活随笔為你收集整理的数据结构(单链表的相关操作)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css3 斜切角/斜边的实现方式来自BA
- 下一篇: 使用multidex解决64K方法引用的