![正序鏈表的建立.doc_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/31/2b11a053-11d1-458a-8ec2-9bc1611075df/2b11a053-11d1-458a-8ec2-9bc1611075df1.gif)
![正序鏈表的建立.doc_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/31/2b11a053-11d1-458a-8ec2-9bc1611075df/2b11a053-11d1-458a-8ec2-9bc1611075df2.gif)
![正序鏈表的建立.doc_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/31/2b11a053-11d1-458a-8ec2-9bc1611075df/2b11a053-11d1-458a-8ec2-9bc1611075df3.gif)
![正序鏈表的建立.doc_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/31/2b11a053-11d1-458a-8ec2-9bc1611075df/2b11a053-11d1-458a-8ec2-9bc1611075df4.gif)
![正序鏈表的建立.doc_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/31/2b11a053-11d1-458a-8ec2-9bc1611075df/2b11a053-11d1-458a-8ec2-9bc1611075df5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、正序鏈表的建立建立正序鏈表請(qǐng)按照輸入整數(shù)的順序建立一個(gè)正序的帶表頭節(jié)點(diǎn)的鏈表。已知程序的基本結(jié)構(gòu)如下,請(qǐng)你編寫 ins_list函數(shù)。 鏈表問題很不懂。求看 ins_list函數(shù)有什么問題,別的都是題目已經(jīng)給出來的代碼。/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */#include stdio.hstruct nodeint data;struct node * next; ;typedef struct node NODE;typedef struct node * PNODE;int main ( )int num=1;PNODE head
2、;head = (PNODE)malloc( sizeof(NODE) );head-next = NULL;head-data = -1;while ( num!=0 )scanf(%d, &num);if ( num!=0 )ins_list( head, num);outlist( head );return 0;outlist( PNODE head )PNODE p;p = head-next;while ( p != NULL )printf(%dn, p-data);p = p-next;ins_list( PNODE h, int num )NODE *q,*p;int n=0
3、;p-next=NULL;p-data=num;for(q=h; q-next!=NULL; n+)q=q-next;q-next=p;分享到 :-解決方案 -那兩個(gè)函數(shù)都沒有返回類型,沒有返回類型的,可以寫void-解決方案 -改了下/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */ /#include stdio.h#include #include struct nodeint data;struct node * next; ;typedef struct node NODE;typedef struct node * PNODE;void
4、 ins_list( PNODE h, int num );void outlist( PNODE head );int main ( )int num=1;PNODE head;head = (PNODE)malloc( sizeof(NODE) );head-next = NULL;head-data = -1;while ( num!=0 )scanf(%d, &num);if ( num!=0 )ins_list( head, num); outlist( head );return 0;void outlist( PNODE head ) PNODE p;p = head-next;
5、while ( p != NULL )printf(%dn, p-data);p = p-next;void ins_list( PNODE h, int num ) NODE *q,*p;p = (PNODE)malloc( sizeof(NODE) ); /p-data=num;for(q=h; q-next!=NULL; )q=q-next;q-next=p;添加p-next=NULL;-解決方案-單是NODE *p;的話只是定義指針,沒有分配內(nèi)存,-解決方案-僅供參考/ 假設(shè)帶表頭結(jié)點(diǎn)的單向鏈表頭指針為 head,試編寫一個(gè)算法將值為 5 的結(jié)點(diǎn)插入到連接表的第 k 個(gè)結(jié)點(diǎn)前,刪除第k
6、 個(gè)節(jié)點(diǎn),并對(duì)該鏈表進(jìn)行排序。#include #include #include #include struct NODE intdata;struct NODE *next; H,*head,*p,*q,*s1,*s2,*s3,*s4,*s; int i,j,k,n,t;int main() srand(time(NULL);/ 填寫頭節(jié)點(diǎn)數(shù)據(jù)H.data=-1;H.next=NULL;head=&H;/ 創(chuàng)建 10 個(gè)節(jié)點(diǎn)的單鏈表p=head;for (i=0;idata=rand()%100;/填寫0.99的隨機(jī)值q-next=NULL;p-next=q;p=q;/ 輸出整個(gè)單鏈表s=
7、head-next;while (1) if (NULL=s) printf(n);break;printf(%02d-,s-data);s=s-next;/ 將值為 5 的結(jié)點(diǎn)插入到單鏈表的第 k 個(gè)結(jié)點(diǎn)前k=3;n=0;p=head;while (1) if (NULL=p) break;n+;if (k=n) q=(struct NODE *)malloc(sizeof(struct NODE);if (NULL=q) return 1;q-data=5;q-next=p-next;p-next=q;break;p=p-next;/ 輸出整個(gè)單鏈表s=head-next;while (1
8、) if (NULL=s) printf(n);break;printf(%02d-,s-data);s=s-next;/ 刪除第 k 個(gè)節(jié)點(diǎn)k=5;n=0;p=head;while (1) if (NULL=p) break;n+;if (k=n) q=p-next;if (q) p-next=q-next;free(q);break;p=p-next;/ 輸出整個(gè)單鏈表=head-next; swhile (1) if (NULL=s) printf(n);break;printf(%02d-,s-data);s=s-next;/ 從小到大排序for (p=head;p!=NULL & p
9、-next!=NULL;p=p-next) for (q=p-next;q!=NULL & q-next!=NULL;q=q-next) if (p-next-data q-next-data) / 交換 data/ printf(swap %02d %02dn,p-next-data,q-next-data);/ t=p-next-data;p-next-data=q-next-data;q-next-data=t;/ 或者/ 交換 next / printf(swap %02d %02dn,p-next-data,q-next-data); s1=p-next; s2=p-next-nex
10、t;s3=q-next; s4=q-next-next;if (s2!=s3) s3-next=s2;q-next=s1;s1-next=s4;p-next=s3; else p-next=s3;s3-next=s1;q=s3;s1-next=s4;/ 輸出整個(gè)單鏈表 / s=head-next; / while (1) / (NULL=s) / printf(n); / break; / if/ printf(%02d-,s-data);/s=s-next; /getchar(); / 輸出整個(gè)單鏈表s=head-next;while (1) if (NULL=s) printf(n);br
11、eak;printf(%02d-,s-data);s=s-next;/ 釋放所有節(jié)點(diǎn)p=head-next;while (1) if (NULL=p) break;q=p-next;free(p);p=q;return 0;/29-82-10-22-71-05-13-60-59-39-/29-82-05-10-22-71-05-13-60-59-39-/29-82-05-10-71-05-13-60-59-39-/05-05-10-13-29-39-59-60-71-82-/-解決方案 -#include stdio.h#include struct node幫你修改了一下int data;s
12、truct node * next; ;typedef struct node NODE;typedef struct node * PNODE;int outlist( PNODE head )PNODE p;p = head-next;while ( p != NULL )printf(%dn, p-data);p = p-next;return 1;void ins_list( PNODE h, int num )NODE * r,* p;r=h;while(r-next!=NULL)r=r-next;p=(PNODE)malloc(sizeof(NODE);p-data=num;p-next=NULL;/p為待插結(jié)點(diǎn)if(h-next=NULL)h-next=p;r=p;elser-next=p;r=r-next;int main ( )int num=1;PNODE head;head =
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 莫公司應(yīng)急預(yù)案事故風(fēng)險(xiǎn)評(píng)估報(bào)告模板
- 目標(biāo)設(shè)定與職業(yè)技能提升的聯(lián)動(dòng)
- 2025年自行車撐架項(xiàng)目投資可行性研究分析報(bào)告
- 農(nóng)田別墅租售合同范本
- 中國修正帶未來趨勢預(yù)測分析及投資規(guī)劃研究建議報(bào)告
- 個(gè)人委托咨詢合同范本
- 借錢押車合同范本
- 人像攝影合同范本
- 書畫代銷合同范本
- 2025年清漆固化劑行業(yè)深度研究分析報(bào)告
- 《預(yù)防流感》主題班會(huì)教案3篇
- 校園招聘活動(dòng)策劃方案(6篇)
- 期末 (試題) -2024-2025學(xué)年教科版(廣州)英語四年級(jí)上冊(cè)
- 解讀國有企業(yè)管理人員處分條例課件
- 湖南省長沙市一中2024-2025學(xué)年高一生物上學(xué)期期末考試試題含解析
- 小孩使用手機(jī)協(xié)議書范本
- 榆神礦區(qū)郭家灘煤礦(700 萬噸-年)項(xiàng)目環(huán)評(píng)
- 2024年200MW-400MWh電化學(xué)儲(chǔ)能電站設(shè)計(jì)方案
- 余土外運(yùn)施工方案
- DB32-T 186-2015建筑消防設(shè)施檢測技術(shù)規(guī)程
- 中考英語1600詞匯對(duì)照表-(帶音標(biāo))
評(píng)論
0/150
提交評(píng)論