


版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、軟基上機(jī)報告鏈表(拓展題)一、程序流程說明二、1)創(chuàng)建一個單鏈表,其數(shù)據(jù)元素為整數(shù), 從鍵盤輸入,輸入0結(jié)束(注意0不放到鏈表內(nèi));三、2)從鍵盤任意輸入一個整數(shù),在單鏈表 中查詢該數(shù),如果單鏈表中已經(jīng)存在這個數(shù),就 調(diào)用刪除函數(shù),刪除該元素所在結(jié)點,并將單鏈 表在刪除前后的數(shù)據(jù)元素依次輸出到屏幕上;四、如果單鏈表中不存在這個數(shù),就調(diào)用插 入函數(shù),將這個數(shù)插入到單鏈表尾,并將單鏈表 在插入前后的數(shù)據(jù)元素依次輸出到屏幕上。五、3)教材第一章習(xí)題第9題(用鏈表實現(xiàn)) ex2_3擴(kuò)展題六、1)刪除單鏈表中全部的負(fù)數(shù)七、2)創(chuàng)建一個雙向鏈表,按照冒泡排序的思 路對這個雙向鏈表進(jìn)行排序,打印排序結(jié)果。注
2、 意,本算法在交換元素時是將鏈點整個交換而不 是將鏈點中的元素值交換。八、九、 十、2_21)創(chuàng)建一個單鏈表,其數(shù)據(jù)元素為整數(shù),從鍵 盤輸入,輸入0結(jié)束(注意0不放到鏈表內(nèi));(if x ! =0, seanf)2 )從鍵盤任意輸入一個整數(shù),在單鏈表中查詢該數(shù),如果單鏈表中已經(jīng)存在這個數(shù), 就調(diào)用刪除函數(shù),刪除該元素所在結(jié)點,并將單鏈表在刪除前后的數(shù)據(jù)元素依次輸出到屏幕上;(search返回重復(fù)的位置,然后刪除(同實驗一)如果單鏈表中不存在這個數(shù),就調(diào)用插入函 數(shù),將這個數(shù)插入到單鏈表尾,并將單鏈表在插 入前后的數(shù)據(jù)元素依次輸出到屏幕上。(同實驗 一,直接加在鏈表末尾)3)判斷插入元素與表內(nèi)元
3、素的大小,如 temp->data<x<temp->link->data ,貝U插入 temp 后2_31)刪除單鏈表中全部的負(fù)數(shù)(逐個判斷是不是 負(fù)數(shù),并且返回負(fù)數(shù)所在位置,刪除過春哥同實 驗一2)創(chuàng)建一個雙向鏈表,按照冒泡排序的思路對 這個雙向鏈表進(jìn)行排序,打印排序結(jié)果。注意, 本算法在交換元素時是將鏈點整個交換而不是 將鏈點中的元素值交換。(從一開始逐個比較, 將比第一位大的與第一位交換位置,一直比較到 最后一位然后開始對于第二位重復(fù)比較)二、程序代碼2_2#in clude<stdio.h>#in clude<malloc.h>#i
4、n clude<stdlib.h>typedef struct Nodeint data;struct Node *li nk;no de;typedef struct Listnode *head;node *tail;int len gth;list;void creat_list(list *table)int x,i;node *temp;table->head=NULL; table->tail=NULL; table->le ngth=O;x=1;sea nf("%d", &x);for(i=0;x!=0;i+)fflush
5、(stdi n);prin tf("n");temp=( no de*)malloc(sizeof( no de); temp->data=x;temp->li nk=NULL;temp->li nk=table->head; table->head=temp;table->le ngth+;sca nf("%d", &x);node * create_ no de(i nt new_no de)node * temp;temp = (no de*)malloc(sizeof( no de); temp->
6、;data = new_no de;temp->li nk = NULL;return temp;void get(list *table,int n)int i,loc=table->le ngth+1;node *temp;node *newno de;temp=table->head;table->le ngth+;newno de=create_ node(n);for(i=2;i<loc;i+) temp=temp->li nk;newno de->li nk = temp->li nk; temp->li nk = newno
7、de;void show_list(list *table)int x,i;node *temp;printf("鏈表為:n");temp=table->head;for(i=1;i <= table->le ngth;i+)x=temp->data;prin tf("%d ",x);temp=temp->li nk;void delete_ no de(list *table,i nt n)int i;node *temp,* t;temp=table->head;if(n=1)temp=temp->li nk
8、; table->head=temp;if(n>2&&n< table->le ngth)temp=table->head;for(i=2;i <n ;i+) temp=temp->li nk;t=temp->li nk;temp->li nk=t->li nk;if(n=table->le ngth) for(i=2;i <n ;i+) temp=temp->li nk;temp->li nk=NULL;if(n=2)temp=table->head;t=temp->li nk;
9、temp->link=t->link;table->length-;void search(list *table,i nt x,i nt a2) int i=0,j=0;node *temp; temp=table->head;for(i=1;i<=table->le ngth;i+) if(x=temp->data)a1=i;j+;temp=temp->li nk;if(j=0)a0=0;a1=0;elsea0=1;void mai n()list table;int m,a2;creat_list (&table);show_lis
10、t(&table);printf("n輸入一個整數(shù) n");sca nf("%d",&m);search(&table,m,a);if(a0=l)delete_node(&table,a1);show_list(&table);elseget(&table,m);show_list(&table);2_2(3)#in clude<stdio.h>#in clude<malloc.h>#in clude<stdlib.h> typedef struct Node i
11、nt data;struct Node *li nk;no de;typedef struct Listnode *head;node *tail;int len gth;list;void creat_list(list *table) int x,i;node *temp; table->head=NULL; table->tail=NULL; table->le ngth=O;x=1;sca nf("%d", &x);for(i=O;x!=O;i+)fflush(stdi n);prin tf("n");temp=( no
12、 de*)malloc(sizeof( no de); temp->data=x;temp->li nk=NULL;temp->li nk=table->head; table->head=temp;table->le ngth+;sca nf("%d", &x);node * create_ no de(i nt new_no de)node * temp;temp = (no de*)malloc(sizeof( no de); temp->data = new_no de;temp->li nk = NULL;r
13、eturn temp;void get(list *table,i nt n)int i,m=0;node *temp;node *newno de;temp=table->head;table->le ngth+;newno de=create_ node(n);if(n< temp->data)newno de->li nk=temp; temp=newno de;elsefor(i=1;m=0;i+)if(iv=table->le ngth&&temp->data <n&&n<=t emp->li
14、 nk->data)newno de->li nk = temp->li nk;temp->li nk = newno de;m=1;else if(i=table->le ngth) temp->li nk=newno de;newno de->li nk=NULL;m=1;temp=temp->li nk;table->le ngth+;void show_list(list *table)int x,i;node *temp;printf("鏈表為:n");temp=table->head;for(i=1;i
15、 <= table->le ngth;i+) x=temp->data; prin tf("%d ",x); temp=temp->li nk;void mai n()list table;int m,a2;creat_list (&table);show_list(&table);printf("n輸入一個整數(shù) n");sca nf("%d",&m);get(&table,m); show_list(&table);2_3(1)#in clude<stdio.h&
16、gt;#in clude<malloc.h>#in clude<stdlib.h> typedef struct Node int data; struct Node *li nk;no de;typedef struct Listnode *head;node *tail;int len gth;list;void creat_list(list *table) int x,i;node *temp; table->head=NULL; table->tail=NULL; table->le ngth=O;x=1;sca nf("%d&qu
17、ot;, &x); for(i=O;x!=O;i+)fflush(stdi n);prin tf("n");temp=( no de*)malloc(sizeof( no de);temp->data=x;temp->li nk=NULL;temp->li nk=table->head; table->head=temp;table->le ngth+;sea nf("%d", &x);node * create_ no de(i nt new_no de)node * temp;temp = (no
18、de*)malloc(sizeof( no de); temp->data = new_no de;temp->li nk = NULL;return temp;void get(list *table,int n)int i,loc=table->le ngth+1;node *temp;node *newno de;temp=table->head;table->le ngth+;newno de=create_ node(n);for(i=2;i<loc;i+)temp=temp->li nk;newno de->li nk = temp-
19、>li nk; temp->li nk = newno de;void show_list(list *table)int x,i;node *temp;printf("n鏈表為:n");temp=table->head;for(i=1;i <= table->le ngth;i+) x=temp->data;prin tf("%d ",x); temp=temp->li nk;void delete_ no de(list *table,int n)int i;node *temp,* t;temp=table
20、->head;if(n=1)temp=temp->li nk; table->head=temp;if(n>2&&n< table->le ngth) temp=table->head; for(i=2;i <n ;i+) temp=temp->li nk; t=temp->li nk; temp->link=t->link;if(n=table->le ngth) for(i=2;i <n ;i+) temp=temp->li nk;temp->li nk=NULL;if(n=2)
21、temp=table->head; t=temp->li nk; temp->link=t->link;table->le ngth-;void search(list *table,i nt a2)int i=0,j=0;node *temp;temp=table->head;for(i=1;i<=table->le ngth;i+) if(temp->data<0)a1=i;j=1;break;temp=temp->li nk;if(j=0)a0=0;a1=0;else a0=1;void paixu(list *table
22、)int i,j,m,u;node *temp,*t,*p;for(i=1;i<=table->le ngth;i+)temp=table->head; for(u=1;u<i;u+) temp=temp->li nk;t=temp;for(j=i;jv=table->le ngth;j+)temp=t;for(m=i;m<=j;m+) temp=temp->li nk;if(t->data<(temp->data) p=temp->li nk; temp->link=t->link; t->li nk=
23、p;void mai n()list table;int m,a2; creat_list (&table); show_list(&table); search(&table,a);for(m=1;a0=1;m+) search(&table,a);if(a0=1)delete_node(&table,a1);show_list(&table);2_3 (2)#in clude<stdio.h>#in clude<malloc.h>#in clude<stdlib.h> typedef struct Node
24、int data;struct Node *li nk,*last;no de;typedef struct Listnode *head;node *tail;int len gth;list;void creat_list(list *table) int x,i; node *temp,*t,*p; table->head=NULL; table->tail=NULL;table->le ngth=O;x=1;sea nf("%d", &x);for(i=0;x!=0;i+)fflush(stdi n);prin tf("n&quo
25、t;);temp=( no de*)malloc(sizeof( no de); temp->data=x;temp->li nk=NULL;temp->li nk=table->head; table->head=temp;table->le ngth+;sca nf("%d", &x);t=( no de*)malloc(sizeof( no de); t=table->head;t->last=NULL;for(i=2;i<=table->le ngth;i+)p=t;t=t->li nk;t-
26、>last=p;void show_list(list *table)int x,i;node *temp;printf("鏈表為:n");temp=table->head;for(i=1;i <= table->le ngth;i+) x=temp->data;prin tf("%d ",x); temp=temp->li nk;void paixu(list *table)int i,j,m;node *temp,*t,*p,*q; temp=table->head;t=temp; for(i=2;i<
27、=table->le ngth;i+) t=t->li nk;if(t->data>temp->data) p=t->li nk;t->li nk=temp->li nk; temp->li nk=p; t->last->li nk=temp; table->head=t; t->last=NULL; temp->last=t->last;p=t;t=temp;temp=t;/*/temp=table->head;for(i=2;i<=table->le ngth;i+)temp=temp->li nk; t=temp;for(j=i+1;j<=table->le ngth;j+) t=t->li nk;if(t->data>temp->data)p=t->li nk;t->li nk
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026屆貴港市重點中學(xué)化學(xué)高一上期末預(yù)測試題含解析
- 超實 用職位應(yīng)對:公務(wù)員面試題實例詳解及模板
- 國際企業(yè)原材料管理圖文
- 幼兒園大班語言活動《丑小鴨》教案
- 現(xiàn)代小說創(chuàng)作實例分享與點評面試題目
- 眼瞼炎癥的藥物治療
- 傷口護(hù)理業(yè)務(wù)學(xué)習(xí)
- 心理健康講解分享
- 夏季衛(wèi)生知識普及課件
- 行星齒輪機(jī)構(gòu)講解
- 溫通刮痧技術(shù)操作流程圖及考核標(biāo)準(zhǔn)
- 19S406建筑排水管道安裝-塑料管道
- 變壓器運行維護(hù)手冊
- GA/T 1161-2014法庭科學(xué)DNA檢驗鑒定文書內(nèi)容及格式
- 云南專升本會計試題
- 民間信仰活動場所信息采集表
- 2023年版義務(wù)教育音樂課程標(biāo)準(zhǔn)(標(biāo)準(zhǔn)版)
- 神華包頭煤化工分公司2013年夏季水平衡測試報告
- 有效咳嗽技術(shù)操作評分標(biāo)準(zhǔn)
- GB∕T 36935-2018 鞋類 鞋號對照表
- 教練技術(shù)一階段講義
評論
0/150
提交評論