




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、2.鏈表的基本操作對鏈表施行的操作有很多種,最基本的操作是在鏈表中插入結(jié)點(diǎn)、在鏈表中刪除結(jié)點(diǎn)、在鏈 表中查找結(jié)點(diǎn)等。鏈表結(jié)點(diǎn)的插入在空鏈表中插入一個結(jié)點(diǎn)空鏈表就是頭指針為空的鏈表。用如下語句申請一個new結(jié)點(diǎn):new=(struct node)calloc(1,sizeof(struct node);為new結(jié)點(diǎn)填充數(shù)據(jù):將要存儲的數(shù)據(jù)對應(yīng)傳遞給new結(jié)點(diǎn)數(shù)據(jù)域的各個成員。修改有關(guān)指針的指向:將new的next成員置空,使new結(jié)點(diǎn)成為鏈表的最后一個結(jié)點(diǎn); 將head指向new結(jié)點(diǎn)。在鏈表的結(jié)點(diǎn)之后插入一個結(jié)點(diǎn)要在鏈表head的C、D結(jié)點(diǎn)之間出入一個new結(jié)點(diǎn),就是將new結(jié)點(diǎn)變成C結(jié)點(diǎn)的下一
2、 個結(jié)點(diǎn),而new結(jié)點(diǎn)的下一個結(jié)點(diǎn)為D結(jié)點(diǎn).操作過程為:使new的指針域存儲結(jié)點(diǎn)D的首地址。使C結(jié)點(diǎn)的指針域存儲結(jié)點(diǎn)new的地址。例2建立學(xué)生成績鏈表,鏈表有3個結(jié)點(diǎn)。#include #define N 3struct s_nodechar num4;int score;struct s_node *next;;main()structsnode*creat_node(void);/*生成鏈表結(jié)點(diǎn)的函數(shù)*/structsnode*creat_list(int n);/*建立鏈表的函數(shù)*/void out_list(struct s_node *head);/*輸出鏈表函數(shù)*/struct s
3、_node *head=NULL;head=creat_list(N);out_list(head);struct s_node *creat_node(void)/*生成鏈表結(jié)點(diǎn)的函數(shù)*/struct s_node *p;int score;fflush(stdin);p=(struct s_node *)calloc(1,sizeof(struct s_node);gets(p-num);scanf(%d”,&score);p-score=score;p-next=NULL;return(p);/*建立鏈表的函數(shù)*/struct s_node * creat_list(int n)stru
4、ct s_node *new,*p;struct s_node *head;int i;if(n=1)new=creat_node();head=new;p=new;for(i=2;inext=new;p=new;if(n=1)return(head);elsereturn(NULL);void out_list(struct s_node *head)/*輸出鏈表函數(shù)*/struct s_node *p;if(head!=NULL)p=head;while(p!=NULL)printf(%s %dn”,p-num,p-score);p=p-next;鏈表結(jié)點(diǎn)的刪除從鏈表中刪除結(jié)點(diǎn),就是撤銷結(jié)
5、點(diǎn)在鏈表中的鏈接,把結(jié)點(diǎn)從鏈表中孤立出來。在鏈表鏈表 中刪除結(jié)點(diǎn)一般有兩個過程:一是把指定的結(jié)點(diǎn)從鏈表中拿下來,它需要通過修改有關(guān)結(jié)點(diǎn) 的指針域來完成;二是釋放該結(jié)點(diǎn)使用的內(nèi)存空間,它需要使用free()函數(shù)來完成。如下是在head鏈表中刪除p結(jié)點(diǎn)的delete_p()函數(shù):void delete_p(struct node *head,struct node *p)struct node *q;if(p=NULL)printf(no foundn);return;if(p=head)head=p-next;q=p;elseq=p-next;p-next=q-next;printf(ndele
6、te :%dn,q-data);free(q);鏈表結(jié)點(diǎn)的查找在鏈表中進(jìn)行查找,就是從鏈表的第一個結(jié)點(diǎn)開始,沿著指針鏈,用查找值與鏈表結(jié)點(diǎn)逐個 比較的過程。找到符合要求的結(jié)點(diǎn)之后,停止查找過程,返回相應(yīng)的結(jié)點(diǎn)指針,否則返回一 個空指針。例3查找值為x的結(jié)點(diǎn)并刪除 #include struct node int data;struct node *next;;struct node * creat_number(int n)/*建立鏈表的函數(shù)*/struct node *new,*p;struct node *head;int i;if(n=1)new=(struct node *)mallo
7、c(sizeof(struct node);new-data=1;new-next=NULL;head=new;p=new;for(i=2;idata=i;new-next=NULL;p-next=new;p=new;if(n=1)return(head);elsereturn(NULL);void out_list(struct node *head)/*輸出鏈表函數(shù)*/struct node *p;if(head!=NULL)p=head;while(p!=NULL)printf(%dn”,p-data);p=p-next;struct node *find_x(struct node *
8、head,int x)/雍鏈表中查找的函數(shù)*/struct node *p,*q;p=q=head;while(p!=NULL&p-data!=x)q=p;p=p-next;if(p=NULL)return(NULL);elsereturn(q);void delete_p(struct node *head,struct node *p)/*在鏈表中刪除 p 結(jié)點(diǎn)的函數(shù)*/struct node *q;if(p=NULL)printf(no foundn);return;if(p=head)head=p-next;q=p;elseq=p-next;p-next=q-next;printf(ndelete :%dn,q-data);free(q);main()int n,x;struct node *head=NUL
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 汽車行業(yè)合同樣本:會員服務(wù)協(xié)議
- 移動基站租賃合同書范本
- 城市老舊小區(qū)消防系統(tǒng)改造項目合同
- 幼兒園臨時教師聘任合同
- 新版民間房產(chǎn)抵押權(quán)轉(zhuǎn)讓合同
- 腎性水腫課件
- 智能化煤礦培訓(xùn)課件下載
- 舊貨零售互聯(lián)網(wǎng)+創(chuàng)新實踐考核試卷
- 搪瓷器的創(chuàng)造思維與創(chuàng)意設(shè)計考核試卷
- 建筑施工現(xiàn)場安全監(jiān)測與預(yù)警考核試卷
- 家譜樹形圖模板
- 【保密工作檔案】外場試驗保密工作方案
- 文苑小學(xué)安全管理網(wǎng)絡(luò)圖0
- 《民法典》婚姻家庭編解讀之夫妻個人財產(chǎn)第1063條PPT課件
- 2 遺傳圖繪制
- 人教部編版二年級語文下冊第六單元15古詩二首精品教案(集體備課)
- 三年級下冊數(shù)學(xué)教案-2.1速度、時間、路程-滬教版
- 隊列動作要領(lǐng)及訓(xùn)練方法
- 中國原發(fā)性醛固酮增多癥診治共識解讀
- 墻面板安裝爬梯驗算
- 矢量分析與場論講義
評論
0/150
提交評論