雙向鏈表的建立、查找、插入、刪除_第1頁
雙向鏈表的建立、查找、插入、刪除_第2頁
雙向鏈表的建立、查找、插入、刪除_第3頁
雙向鏈表的建立、查找、插入、刪除_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、/*雙鏈表的操作:查詢、刪除、顯示、插入。*雙向鏈表克服單向鏈表向前查找結點需要執(zhí)行時間O(n)的缺點*2008.1.17*/#include#include#include#defineN10typedefstructnode*ptNode;structnodecharname20;ptNodelLink,rLink;/*雙鏈表的結構定義*/*雙鏈表的創(chuàng)建*/ptNodeCreat(intn)ptNodehead,ptr,newnode;inti;if(head=(ptNode)malloc(sizeof(node)=NULL)printf(cannotfindspace!n);exit(0

2、);head-name0=0;/頭結點初始化head-lLink=NULL;head-rLink=NULL;ptr=head;for(i=0;irLink=newnode;/連接新結點printf(Pleaseinputthe%dmansname:,i+1);scanf(%s,newnode-name);newnode-lLink=ptr;/定義newnode的連接關系newnode-rLink=NULL;ptr=newnode;/endforhead-lLink=newnode;ptr-rLink=head;return(head);/*查找名字的位置*/ptNodeSearch(ptNod

3、ehead,char*pEle)ptNodeptr;char*pTemp;ptr=head-rLink;while(ptr!=head)/以是否回到頭結點為結束條件pTemp=ptr-name;/pTemp暫存結點的名字地址if(strcmp(pTemp,pEle)=0)return(ptr);elseptr=ptr-rLink;指向下一結點printf(cannotfinddata!n);returnNULL;/在選定名字前插入voidInsert(ptNodehead,char*prior,char*insertEle)ptNodeptr,newnode;if(ptr=Search(hea

4、d,prior)=NULL)printf(Cantfindthedatatoinsert!n);exit(0);if(newnode=(ptNode)malloc(sizeof(node)=NULL)printf(Cantarrangememoryspace!n);exit(0);strcpy(newnode-name,insertEle);給新結點加載值newnode-lLink=ptr-lLink;前指針指向前結點,先處理以新結點為中心前后關系newnode-rLink=ptr;新結點后指針指向定位結點(ptr-lLink)-rLink=newnode;/前結點的由指針指向新結點ptr-l

5、Link=newnode;/*打印輸出*/voidPrint(ptNodehead)ptNodeptr;ptr=head-rLink;printf(nNowthedoublelistis:n);while(ptr!=head)printf(%s,ptr-name);ptr=ptr-rLink;printf(n);/*刪除*/voidDelete(ptNodeptr)(ptr-rLink)-lLink=ptr-lLink;/(ptr-rLink)-lLink下一結點的左指針被賦前指針(ptr-lLink)-rLink=ptr-rLink;free(ptr);/*主函數*/voidmain(voi

6、d)intnumber;charstudName20;charinsertName20;ptNodehead,searchpoint;number=N;puts(Pleaseinputthesizeofthelist:);scanf(%d,&number);head=Creat(number);Print(head);printf(nPleaseinputthenamewhichyouwanttofind:n);scanf(%s,studName);searchpoint=Search(head,studName);printf(thenameyouwanttofindis:%sn,searchpoint-name);Delete(searchpoint);Print(head);printf(Beforewhichelementdoyouwanttoinsert:n);scanf(%s,studName);printf(Enterthestu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論