(完整版)數(shù)據(jù)結(jié)構(gòu)線性表的主要程序代碼_第1頁(yè)
(完整版)數(shù)據(jù)結(jié)構(gòu)線性表的主要程序代碼_第2頁(yè)
(完整版)數(shù)據(jù)結(jié)構(gòu)線性表的主要程序代碼_第3頁(yè)
(完整版)數(shù)據(jù)結(jié)構(gòu)線性表的主要程序代碼_第4頁(yè)
(完整版)數(shù)據(jù)結(jié)構(gòu)線性表的主要程序代碼_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、數(shù)據(jù)結(jié)構(gòu)順序表的主要代碼( LIZHULIN )1 /* 有頭結(jié)點(diǎn)的單鏈表的初始化、建立(表頭插入、表尾插入) 、求長(zhǎng)度、插入、刪除、輸出 */*單鏈表的初始化、建立、輸出 */#include#includetypedef struct Lnode /* 定義線性表的單鏈表存儲(chǔ)結(jié)構(gòu)*/int data;struct Lnode *next;LinkList;/*單鏈表的初始化 */- 10 -Initlist(LinkList *L) /* 動(dòng)態(tài)申請(qǐng)存儲(chǔ)空間 */L = (LinkList *)malloc(sizeof(struct Lnode);/* 建立頭結(jié)點(diǎn) */ L-next =

2、NULL;/* 建立一個(gè)帶頭結(jié)點(diǎn)的單鏈表,在表尾插入*/Create_L(LinkList *L,int n)LinkList *p,*q; int i;Initlist(L); /*單鏈表初始化 */q=L;printf(input the valuen);for(i = n;i0;-i)p = (LinkList*)malloc(sizeof(struct Lnode);scanf(%d,&p-data); /*輸入元素值*/q-next = p;p-next = NULL;q=p;/* 插入到表尾*/ /* Create_L */*/* 建立一個(gè)帶頭結(jié)點(diǎn)的單鏈表,在表頭插入 Create

3、_L(LinkList *L,int n) LinkList *p; int i;Initlist(L); /*單鏈表初始化/* 需要注意第一個(gè)數(shù)據(jù)插入時(shí)的情況/*Insert the Firset nodep = (LinkList*)malloc(sizeof(struct Lnode);printf(input the valuen);scanf(%d,&p-data); /*輸入元素值L-next = p;p-next = NULL;/* 將第二個(gè)及后面的數(shù)據(jù)插入for(i = n-1;i0;-i)p = (LinkList*)malloc(sizeof(struct Lnode);p

4、rintf(input a valuen);scanf(%d,&p-data); /*輸入元素值p-next = L-next;L-next = p;/* 插入到表頭 /* Create_L */求單鏈表的長(zhǎng)度 */*int Length_LinkList(LinkList *L) LinkList *p;int i=0;p=L-next;while(p!=NULL) i+;p=p-next;return i;/*Length_LinkList*/*在第 i 個(gè)結(jié)點(diǎn)前插入數(shù)據(jù)x */Insert_LinkList(LinkList *L, int i, int x) LinkList *p,*

5、s;int j=0;p=L;/* 尋找第 i 個(gè)結(jié)點(diǎn) */while(jnext;if (!p) return 0;/* 如果表長(zhǎng)小于 i ,則無(wú)意義*/* 插入元素 x */s=(LinkList *)malloc(sizeof(struct Lnode);s-data=x;s-next=p-next;p-next=s;/*刪除第 i 個(gè)元素,并用y 將其值返回 */int Delete_LinkList(LinkList *L, int i) LinkList *p,*q; int y;int j=0;p=L;/* 尋找第 i 個(gè)結(jié)點(diǎn) */while(jnext;if (!p) retur

6、n 0;/* 如果表長(zhǎng)小于i ,則無(wú)意義*/q=p-next;y=q-data;p-next=q-next;free(q) ;return y; /*Delete_LinkList*/*單鏈表值的輸出*/void display(LinkList *L)/* 字母鏈表的輸出 */LinkList *p; p=L-next; while (p!=NULL) printf(%d ,p-data);p=p-next;/* 主程序 */ main()LinkList *L;int len;int n=0;int x=15;int y;int i=4;L = (LinkList*)malloc(size

7、of(struct Lnode);/*L-data = 0;*/L-next =NULL;printf(input the length of L ,nn);scanf(%d,&n);printf(n);Create_L(L,n);Insert_LinkList(L, i, x);/* y=Delete_LinkList(L,i);printf(the delete elment is y=%dn,y);len=Length_LinkList(L);printf(the length of L is %d,len);*/display(L);getch();2 /* 無(wú)頭結(jié)點(diǎn)的單鏈表建立、插入

8、、求長(zhǎng)度、插入、刪除、輸出 * /#include#includetypedef struct Lnode /* 定義線性表的單鏈表存儲(chǔ)結(jié)構(gòu)*/int data;struct Lnode *next;LinkList;/*Create */Link_Creat(LinkList *L,int n)LinkList *q,*p;int i;printf(input the datan);scanf(%d,&L-data);p=L;for(i=2; idata);p-next=q;q-next=NULL;p=q;/*OutPut*/Link_Display(LinkList *L)LinkList

9、 *p;p=L;while(p!=NULL)printf(%d ,p-data); p=p-next;/*Main()*/main() LinkList *L;int n;L=(LinkList *)malloc(sizeof(struct Lnode);L-data=0;L-next=NULL;printf(Please input the length of LinkList, nn);scanf(%d,&n);Link_Creat(L,n);Link_Display(L);getch();*順序表的建立、查找、插入運(yùn)算*/#include #include typedef int dat

10、atype;#define list_maxsize 20/*define for node struct */ typedef structdatatype datalist_maxsize;int length;SqList;/* InitList*/void InitList(SqList *L)L-length = 0; /*Creat SqList*/ void Create_SqList(SqList *L) int i=0;InitList(L);printf(input SqList.datan);scanf(%d,&L-data0);while(L-datai!=-1)+i;

11、scanf(%d,&(L-datai);L-length = i;/*the length of SqList*/ int ListLength(SqList *L) return L-length;/* GetElemL-datai*/int GetElem(SqList *L, int i)if(iL-length) printf(Position Error);return;elsereturn L-datai-1; void Display_SqList(SqList *L)/*Output the SqList*/int i,n;n=ListLength(L);printf(the

12、length is %d ,n);for(i=0;idatai); /*Main()*/ main()SqList *L;/*printf(input the length of SqListn);scanf(%d,&len);*/Create_SqList(L);Display_SqList(L);getch();4*順序表的歸并運(yùn)算*/#include #include typedef int datatype;#define list_maxsize 20/*define for node struct */typedef structdatatype datalist_maxsize;

13、int length;SqList;/* InitList*/void InitList(SqList *L)L-length = 0;/*Creat SqList*/void Create_SqList(SqList *L)int i=0;InitList(L);printf(input the data of SqListn);scanf(%d,&L-data0);while(L-datai!=-1)+i;scanf(%d,&(L-datai);L-length = i;/*the length of SqList*/ int ListLength(SqList *L) return L-

14、length;/* GetElemL-datai*/int GetElem(SqList *L, int i) if(iL-length) printf(Getelem Position Error); return;return L-datai-1; /* Insert Operation */ void ListInsert(SqList *L,int i, int x)SqList *q, *p;if(iL-length)printf(the insert position error);return ;q = &(L-datai-1);/*q 為插入位置*/for(p=&(L-data

15、L-length-1); p=q; -p)*(p+1) = *p;L-datai-1 = x;+L-length; /* LA and LB Merged LC */ void MergeList(SqList *LA,SqList *LB,SqList *LC) int La_len,Lb_len,ai,bj;int i,j;int k;i=j=1;InitList(LC);La_len = ListLength(LA);Lb_len = ListLength(LB);LC-length = La_len+Lb_len;/*for(k=0;klength;k+)LC-datak = 0; *

16、/k=0;while(i=La_len)&(j=Lb_len) ai= GetElem(LA, i);bj= GetElem(LB, j);if(aibj)+k;ListInsert(LC,k,ai);+i;elseif(ai=bj)+k;ListInsert(LC,k,ai);+k;ListInsert(LC,k,bj);+i;+j;else+k;ListInsert(LC,k,bj);+j;while(i=La_len)/*Append the residual node into LA */ ai= GetElem(LA, i);+i;+k;ListInsert(LC,k,ai);whi

17、le(jlength = La_len+Lb_len; void Display_SqList(SqList *L)/*Output the SqList*/int i,n;n=ListLength(L);printf(the length is %d ,n);for(i=0;idatai);/*Main()*/main()SqList *LA , *LB, *LC;Create_SqList(LA);Create_SqList(LB);MergeList(LA,LB,LC);Display_SqList(LC); getch();- 11 -*用帶頭結(jié)點(diǎn)的循環(huán)單鏈表解決約瑟夫問(wèn)題*#incl

18、ude#includetypedef struct Lnode /* 定義線性表的單鏈表存儲(chǔ)結(jié)構(gòu)*/int data;struct Lnode *next;LinkList;/*單鏈表的初始化 */Initlist(LinkList *L) /* 動(dòng)態(tài)申請(qǐng)存儲(chǔ)空間 */L = (LinkList *)malloc(sizeof(struct Lnode);/* 建立頭結(jié)點(diǎn) */ L-next = L;/*建立一個(gè)帶頭結(jié)點(diǎn)的循環(huán)單鏈表,數(shù)據(jù)值為1,2,3,n,在表尾插入*/Create_L(LinkList *L,int n) LinkList *p; int i;Initlist(L); /*

19、單鏈表初始化p=L;for(i = n;i0;-i)q = (LinkList*)malloc(sizeof(struct Lnode);q-data = i; /*輸入元素值p-next =qq-next = L;/* 插入到表尾 /* Create_L */*void display(LinkList *L) LinkList *p;p=L-next;while (p-next!=L)單鏈表值的輸出 */* 字母鏈表的輸出 */- 16 -printf(%d ,p-data);p=p-next;/* 主程序 */ main()LinkList *L;int n;L = (LinkList*)malloc(sizeof(struct Lnode);/*L-data = 0;*/L-next =L;printf(input the length of L ,nn);scanf(%d,&n);printf(

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論