




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
實(shí)現(xiàn)功能:1. 創(chuàng)建一個(gè)新鏈表。2. 插入節(jié)點(diǎn)。3. 刪除節(jié)點(diǎn)。4. 插入法排序鏈表(從小到大)。5. 選擇法排序鏈表(從小到大)。6. 顯示當(dāng)前鏈表。0. 退出程序。./*單向鏈表的相關(guān)操作*/* 2010.03.27 */.#include stdio.h#include malloc.h#include stdlib.h#define null 0int length;struct Listint data;struct List *next;List;struct List * InitList()struct List *head,*p,*q;int d;printf(n創(chuàng)建一個(gè)鏈表:nn);head=(struct List *)malloc(sizeof(struct List);head-next=null; q=head;scanf(%d,&d);while(d0) p=(struct List *)malloc(sizeof(struct List); p-next=null; p-data=d; q-next=p; q=p; scanf(%d,&d); length+;return head;void ListInsert(struct List *head,int i,int e)struct List *p,*q;int j=1;if(i0&i0) p=head; while(jnext; j+; q=(struct List *)malloc(sizeof(struct List); q-data=e; q-next=p-next; p-next=q; length+=1;else printf(n對(duì)不起您所輸入的節(jié)點(diǎn)位置或數(shù)據(jù)出錯(cuò),數(shù)據(jù)插入失敗!n);int ListDelete(struct List *head,int i,int e)struct List *p,*q;int j=0;if(i0&i=length) p=head; while(jnext; j+; e=p-next-data; q=p-next; p-next=q-next; free(q); length-=1; return e;else printf(n對(duì)不起您所輸入的節(jié)點(diǎn)位置出錯(cuò),數(shù)據(jù)刪除失敗!n); return null; void ListInsertSort (struct List *head)struct List *p,*q,*s;q=head-next-next;head-next-next=null;while(q) s=q-next; p=head; while(p-next&p-next-datadata) p=p-next; q-next=p-next; p-next=q; q=s;void ListChooseSort (struct List *head)struct List *p,*q;int t;for(p=head-next;p-next;p=p-next) for(q=p-next;q;q=q-next) if(p-dataq-data) t=p-data; p-data=q-data; q-data=t; void print(struct List *head)struct List *p;if(head) p=head-next; while(p) printf(%5d ,p-data); p=p-next; printf(n);void main()struct List *head;int i,e,n;head=null;do system(cls); printf(nn 單向鏈表的相關(guān)操作nnn); printf( 1. 創(chuàng)建一個(gè)新鏈表。nn); printf( 2. 插入節(jié)點(diǎn)。nn); printf( 3. 刪除節(jié)點(diǎn)。nn); printf( 4. 插入法排序鏈表(從小到大)。nn); printf( 5. 選擇法排序鏈表(從小到大)。nn); printf( 6. 顯示當(dāng)前鏈表。nn); printf( 0. 退出程序。nnnn); printf( 請(qǐng)選擇所要操作的項(xiàng)目:); scanf(%d,&n); switch(n) case 1: system(cls); head=InitList(); printf(n您所創(chuàng)建的鏈表為:); print(head); printf(n); system(pause); break; case 2: system(cls); if(length) printf(n當(dāng)前鏈表為:); print(head); printf(n請(qǐng)輸入要插入的節(jié)點(diǎn)位置及要插入的數(shù)據(jù):); scanf(%d%d,&i,&e); ListInsert(head,i,e); printf(n插入節(jié)點(diǎn)后的鏈表為:); print(head); else printf(n請(qǐng)先創(chuàng)建鏈表,再執(zhí)行此操作!nn); printf(n); system(pause); break; case 3: system(cls); if(length) printf(n當(dāng)前鏈表為:); print(head); printf(n請(qǐng)輸入要?jiǎng)h除的節(jié)點(diǎn)位置:); scanf(%d,&i); e=ListDelete(head,i,e); printf(n您所刪除的節(jié)點(diǎn)處的數(shù)據(jù)為%dnn,e); if(length) printf(刪除節(jié)點(diǎn)后的鏈表為:); else printf(刪除節(jié)點(diǎn)后的鏈表為空表。); print(head); else printf(n請(qǐng)先創(chuàng)建鏈表,再執(zhí)行此操作!nn); printf(n); system(pause); break; case 4: system(cls); if(length) printf(n當(dāng)前鏈表為:); print(head); ListInsertSort(head); printf(n排序后的鏈表為:); print(head); else printf(n請(qǐng)先創(chuàng)建鏈表,再執(zhí)行此操作!nn); printf(n); system(pause); break; case 5: system(cls); if(length) printf(n當(dāng)前鏈表為:); print(head); ListChooseSort(head); printf(n排序后的鏈表為:); print(head); else printf(n請(qǐng)先創(chuàng)建鏈表,再執(zhí)行此操作!nn); printf(n); system(pause); break; case 6: system(cls); if(length) printf(n當(dāng)前鏈表為:); print(head); else printf(n請(qǐng)先創(chuàng)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 游戲開發(fā)臨時(shí)測(cè)試工程師項(xiàng)目合同
- 子女醫(yī)療費(fèi)用分擔(dān)及疾病防治服務(wù)合同
- 企業(yè)管理核心要素與實(shí)踐策略
- 架子工高空作業(yè)安全責(zé)任及勞務(wù)派遣合同
- 《成交策略解析與應(yīng)用》課件
- 影視劇化妝間租賃與化妝服務(wù)一體化合同
- 《心情與養(yǎng)生》課件2
- 《肺部聽診解析》課件
- 公交公司安全管理體系構(gòu)建與實(shí)施
- 《心理健康與生活品質(zhì)》課件
- 改革開放與新時(shí)代智慧樹知到期末考試答案2024年
- 卵圓孔未閉課件
- 腹腔穿刺術(shù)評(píng)分表
- 庫侖定律(公開課)完整版
- 海洋塑料垃圾治理國(guó)際協(xié)作
- 紀(jì)檢監(jiān)委開展保密知識(shí)講座
- 食材配送投標(biāo)方案(技術(shù)標(biāo))
- 哈薩克族服飾.課件
- 自動(dòng)線安全操作規(guī)程
- (10)-3.12 管理載體寓教于管法
- 聚酯生產(chǎn)技術(shù) 聚酯的性能與用途
評(píng)論
0/150
提交評(píng)論