




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
實(shí)現(xiàn)功能:1. 創(chuàng)建一個新鏈表。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)建一個鏈表: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對不起您所輸入的節(jié)點(diǎn)位置或數(shù)據(jù)出錯,數(shù)據(jù)插入失?。);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對不起您所輸入的節(jié)點(diǎn)位置出錯,數(shù)據(jù)刪除失?。); 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)建一個新鏈表。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( 請選擇所要操作的項(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請輸入要插入的節(jié)點(diǎn)位置及要插入的數(shù)據(jù):); scanf(%d%d,&i,&e); ListInsert(head,i,e); printf(n插入節(jié)點(diǎn)后的鏈表為:); print(head); else printf(n請先創(chuàng)建鏈表,再執(zhí)行此操作!nn); printf(n); system(pause); break; case 3: system(cls); if(length) printf(n當(dāng)前鏈表為:); print(head); printf(n請輸入要刪除的節(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請先創(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請先創(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請先創(chuàng)建鏈表,再執(zhí)行此操作!nn); printf(n); system(pause); break; case 6: system(cls); if(length) printf(n當(dāng)前鏈表為:); print(head); else printf(n請先創(chuàng)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 直面挫折 健康成長主題班隊(duì)會
- 幼兒園小班健康跳跳糖
- 論文 投稿雜志社
- 腫瘤溶解綜合征應(yīng)急演練
- 員工談話培訓(xùn)課件
- 建筑熱水系統(tǒng)課件
- 小學(xué)開學(xué)教學(xué)主任培訓(xùn)會
- 婦科護(hù)理文書書寫規(guī)范
- 《數(shù)據(jù)庫原理及MySQL應(yīng)用(微課版)》課件 第9章數(shù)據(jù)查詢
- 窒息癥的急救護(hù)理
- 2025年上海市研發(fā)公共服務(wù)平臺管理中心招聘題庫帶答案分析
- 2025年輕人情緒消費(fèi)趨勢報告-抖音商城xsocialbeta-202506
- 工程保險課件
- 培訓(xùn)中心項(xiàng)目管理制度
- 高中教科研課題:《新課程背景下高中語文情境教學(xué)改革研究》課題工作匯報
- 金融公司干股協(xié)議書
- 2025益陽事業(yè)單位筆試真題
- 2025年寧波市奉化區(qū)紅果文體產(chǎn)業(yè)運(yùn)營管理有限公司招聘筆試參考題庫含答案解析
- 國際壓力性損傷潰瘍預(yù)防和治療臨床指南(2025年版)解讀
- 行政管理過程中道德與法律的關(guān)系試題及答案
- 2025年初中地理學(xué)業(yè)水平考試(八年級)模擬卷【內(nèi)蒙古專用】(含解析)
評論
0/150
提交評論