單鏈表基本操作實(shí)驗(yàn)_第1頁
單鏈表基本操作實(shí)驗(yàn)_第2頁
單鏈表基本操作實(shí)驗(yàn)_第3頁
單鏈表基本操作實(shí)驗(yàn)_第4頁
單鏈表基本操作實(shí)驗(yàn)_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、實(shí)驗(yàn)2鏈表的操作實(shí)驗(yàn)內(nèi)容:1) 基礎(chǔ)題:編寫鏈表基本操作函數(shù),鏈表帶有頭結(jié)點(diǎn)(1) creatust_h()/用頭插法建立鏈表(2) createust_t(>/用尾插法建立鏈表(3) insertlist ()向鏈表的指定位置插入元素(4) deleteust ()刪除鏈表中指定元素值(5) findlist ()查找鏈表中的元素(6) outputlist ()輸出鏈表中元素2) 提高題:(1) 將一個(gè)頭節(jié)點(diǎn)指針為heada的單鏈表a分解成兩個(gè)單鏈表a和b,其頭結(jié)點(diǎn)指針分 別為heada和headb,使得a表中含有原單鏈表a中序號(hào)為奇數(shù)的元素,b表中含有原鏈表 a中序號(hào)為偶數(shù)的元素,

2、且保持原來的相對(duì)順序。(2) 將一個(gè)單鏈表就地逆置。即原表(al, a2,。an),逆置后新表 (an,an-1,ooooooo a 1)程序功能:單鏈表基本功能操作 編程者 :楊天嘯円期:2016-04-14版本號(hào):3.0*/include <stdio.h>/include <stdlib.h>typedef struct list int data;struct list *next;list;void creatlist_h(list *l) /頭插法 int i = 0; int n = 0; int goal;list *p;printf(n請輸入數(shù)據(jù)的個(gè)數(shù)

3、人n"> scanf("%d",&n);l -> next = null; for(i=0;i<n;i+)printf("請輸入第(1 個(gè)數(shù):n",i+l; scanf("%d'&goal);p = (struct list*)malloc(sizeof(struct list); p -> data = goal;p -> next = l->next; /將l指向的地址賦值給p; l -> next = p;void createlist_t(list *l) /尾

4、插法int i; int n; int goal;list *p;list *q=l;printf("請輸入數(shù)據(jù)的個(gè)數(shù):«1"); scanf(,,o/od'&n);for (i=0;i<n;i+)printf("請輸入第<1 個(gè)數(shù):n",i+l); scanf("%d",&goal);p = (struct list*)malloc(sizeof(struct list); p -> data = goal; q -> next = p;q = p;q -> next

5、= null;void lnslist(list *l,int i'int e) /插入list *s;list *p = l; int j = 0;while (p&&j<i-l)p = p->next;+j;s = (struct list*)malloc(sizeof(struct list); s -> data = e;/插入 l 中s -> next = p -> next; p -> next = s;return;void deletelist(list*lzint e) /刪除 list *q;list *p = l

6、;while (p->next&&p->next->data!=e) p = p -> next;if (!(p->next)printf("不存在該元素!n"); exit(o);q = p -> next; p -> next = q->next; e = q -> data; free(q);return;void findlist(list*l,int e)/查找元素intj = 1;list *p = l->next;while (p&&p->data!=e)p =

7、p->next;+j;if(!p)printf("不存在該元素!n"); exit ;printf("您查找的元素位置為:%dn"j);return; void display(list *l)/輸出鏈表list *p = l->next;printf("您輸入的數(shù)據(jù)為:n");while (p!=null)printf ("%d ",p->data); p = p -> next;printf("n");void lnverse(list*l)/申鏈表就地逆置list

8、*q;list *p = l->next;l-> next = null;while (p != null)q = p-> next;/q指針保留原鏈表當(dāng)前處理節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)p -> next = l-> next;/將當(dāng)前處理節(jié)點(diǎn)p插入到逆置l的表頭l -> next = p;p = q;/p指向下一個(gè)待插入的節(jié)點(diǎn)void discreat(list*l)/鏈表拆分int i = 0;/i記錄表a中結(jié)點(diǎn)的序號(hào)list *p;list *b = (struct list*)malloc(sizeof(struct list); /創(chuàng)建 b 表表頭b -&g

9、t; next = null; list*ra = l,*rb = b;表的尾結(jié)點(diǎn)p = l -> next;l -> next = null;/b表初始化"ra和rb將分別指向?qū)?chuàng)建的a表和b/p指向待處理的結(jié)點(diǎn)/置空新的a表while (p != null)i+;if (i%2 = 0)rb -> next = p; rb = p;elsera -> next = p;/序號(hào)加1/處理序號(hào)為偶數(shù)的鏈表結(jié)點(diǎn) /若b表尾描入新結(jié)點(diǎn) /八b指向新的尾結(jié)點(diǎn)/處理原序號(hào)為奇數(shù)的結(jié)點(diǎn) /在a表尾插入新結(jié)點(diǎn)ra = p;p = p->next;/將p指向新的待處理

10、結(jié)點(diǎn)ra -> next = null; rb > next = null; p = l -> next;printf("奇數(shù)位數(shù)據(jù)為:n");/輸出奇數(shù)位數(shù)據(jù)while (p != null)printf ("%d h,p -> data); p = p -> next;printf("n");list *q = b->next;printf("偶數(shù)位數(shù)據(jù)為:n-);/輸出偶數(shù)位數(shù)據(jù)whilefq != null)printf ("%d ",q->data); q = q

11、 -> next;printf("n"); return; int main()int n; int i; int e; int no;/循壞開始ust*l= (struct list*)malloc(sizeof(struct list); ust*b = (struct list*)malloc(sizeof(struct list); char yes_no = 'y'while(yes_no='y'| |yes_no='y') system("cls");pdntf("ttt 卜一-

12、ixn11);/交互式界面printf("ttt|單鏈表基本操作lnh);printf("ttt|-|n");printf("ttt|1-火插法建立鏈表lnn);printf("ttt|2-尾差法建立鏈表lnh);printfc'ttt|3指定位置插入lnm);printf("ttt|4-指定元素刪除lnm);printf("ttt|5-杏找鏈表元素ln");printf("ttt|6輸出鏈表元素lnn);printf("ttt|7單鏈表就地逆3lnn);printf(uttt|8單鏈表

13、拆分ln");printf("ttt|0退出lnh);printf("ttt 卜-ixn11);printfc'wtl計(jì)科14-2第5組,'八 ln");printf("ttt 卜-ixn'1);printf("請選擇運(yùn)算方式:n"scant (,,0/od",&no);switch(no)case 1:creatlist_h(l);display(l);break;case 2:createlist_t(l);display(l);break;case 3:printf(n請輸入需

14、要插入元素的位置:n"); scanf(,,o/od",&i);printf("請輸入需要插入的元素:n"); scanf("%d",&e);lnsust(l,i,e);display(l);break;case 4:printf("請輸入需要?jiǎng)h除的元素:n"> scanf(n%d",&e>deletelist(l,e);display(l);break;case 5:printf("請輸入需要杏找的元素:n"); scanf("%d",&e);findlist(l,e);display(l);break;case 6:display(l);break;case 7:inverse(l);display(l);break;case 8:discreat(l);break;case 0:system

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論