




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、雙向循環(huán)鏈表的基本操作#include using namespace std;struct DblNode int data; DblNode *rLink; DblNode*lLink;class DblListpublic:DblList(; DblList(; bool IsEmpty(; int GetLength(; void Output(; void ClearList(; bool HeadInsert(int e;bool TailInsert(int e; bool Insert(int pos,int e; int Locate(int e; bool DeleteNo
2、de(int pos,int&e;bool Insert(int i,const int& x,int d;bool SetData(int pos,int e; DblNode *GetNode(int pos;void Meun(;private:DblNode *first; DblNode *last; int Length;#include"DblList.h"DblList:DblList(first=new DblNode;if(!firstexit(-1;first->rLink=first;first->lLink=first;
3、last=first;Length=0;DblList:DblList(DblNode *p1=first;DblNode *p2;while(p1!=lastp2=p1;p1=p1->rLink;delete p2;delete last;bool DblList:IsEmpty(if(Length=0 return true;return false;bool DblList:HeadInsert(int eDblNode *p=new DblNode;if(!preturn false;p->data=e;p->rLink=NULL;p->lLink=NULL;i
4、f(Length=0p->rLink=first;p->lLink=first;first->rLink=p;first->lLink=p;last=p;elsep->rLink=first->rLink;p->lLink=first;first->rLink->lLink=p;first->rLink=p;Length+;return true; bool DblList:TailInsert(int eDblNode *p=new DblNode;if(!preturn false;p->data=e;p->rLink
5、=NULL;p->lLink=NULL;last->rLink=p;p->lLink=last;last=p;last->rLink=first;first->lLink=last;Length+;return true;bool DblList:Insert(int pos,int eif(pos<=1return HeadInsert(e;else if(pos>Lengthreturn TailInsert(e;elseDblNode*p1=GetNode(pos-1;DblNode*p2=GetNode(pos;DblNode*p=new Db
6、lNode;if(!preturn false;p->data=e;p->rLink=p2;p->lLink=p1;p2->lLink=p;p1->rLink=p;Length+;return true;bool DblList:DeleteNode(int pos,int&eif(pos<1|pos>Lengthreturn false;DblNode*p1=GetNode(pos-1;DblNode*p2=GetNode(pos;if(p2=lastdelete p2;Length-;last=p1;last->rLink=first
7、;first->lLink=last;elsep1->rLink=p2->rLink;p2->rLink->lLink=p1;delete p2;Length-;return true;int DblList:Locate(int eint i;DblNode*p=first->rLink;for(i=1;i<=Length;i+if(p->data=e return i;p=p->rLink;return 0;DblNode* DblList:GetNode(int posif(pos>Length|pos<0 return
8、NULL;DblNode *p=first;for(int i=0;i p=p->rLink;return p;bool DblList:SetData(int pos,int eif(pos<1|pos>Lengthreturn false;DblNode*p=GetNode(pos;if(!p return false;p->data=e;return true;void DblList:ClearList(DblNode*p1=first->rLink;DblNode*p2;while(p1!=firstp2=p1;p1=p1->rLink;delet
9、e p2;first->rLink=first;first->lLink=first;last=first;Length=0;int DblList:GetLength(return Length;void DblList:Output(cout<<"鏈表中的元素:"DblNode *p=first->rLink;while(p!=firstcout< data<< ' 'p=p->rLink;cout< void DblList:Meun(cout<<"*雙向循環(huán)鏈表的基本操
10、作*"< cout<<" 1-雙向循環(huán)鏈表全部數(shù)據(jù)的輸出"< cout<<" 2-雙向循環(huán)鏈表插入元素"< cout<<" 3-雙向循環(huán)鏈表刪除元素"< cout<<" 4-雙向循環(huán)鏈表查找元素"< cout<<" 5-雙向循環(huán)鏈表元素的修改"< cout<<" 6-雙向循環(huán)鏈表的長度"< cout<<" 7-雙向循環(huán)鏈表清空&qu
11、ot;< cout<<" 0-退出 "< cout<<"*"< cout<<"請輸出功能序號:"#include"DblList.h"void main(DblList dlist;int choice,x,pos,n;dlist.Meun(;cin>>choice;while(choice!=0switch(choicecase 1:dlist.Output(;break;case 2:cout<<"*"< c
12、out<<"1-雙鏈表頭部插入一個元素"< cout<<"2-雙鏈表尾部插入一個元素"< cout<<"3-雙鏈表任意位置插入元素"< cout<<"*"< cout<<"請選擇:"cin>>n;if(n=1cout<<"請輸入頭部要插入的元素值:"cin>>x;dlist.HeadInsert(x;else if(n=2cout<<"
13、請輸入尾部要插入的元素值:"cin>>x;dlist.TailInsert(x;elsecout<<"請輸入位序和元素值:"cin>>pos>>x;dlist.Insert(pos,x;break; break;case 3:cout<<"請輸入要刪除的結點位序:"cin>>pos;dlist.DeleteNode(pos,x;break;case 4:cout<<"*"< cout<<"1-按元素的數(shù)值查找&qu
14、ot;< cout<<"2-按元素的位置查找"< cout<<"*"< cout<<"請選擇:"cin>>n;if(n=1cout<<"請輸入要查找的元素值:"cin>>x;cout<<"位置為:"< elsecout<<"請輸入所需查找元素的位置:"cin>>pos;DblNode *p=dlist.GetNode(pos;if(pcout<<"第"< " 個元素值為: " < data< break;case 5:cout<<"請輸入要改變結點的位序和改制后
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 辦公效率提升解決方案與實踐案例
- 阜陽2025年安徽阜陽市市直事業(yè)單位招聘8人筆試歷年參考題庫附帶答案詳解
- 菏澤2025年山東菏澤市牡丹區(qū)事業(yè)單位招聘初級綜合類崗位57人筆試歷年參考題庫附帶答案詳解
- 紹興2025年浙江紹興市柯橋區(qū)司法局編外工作人員招聘16人筆試歷年參考題庫附帶答案詳解
- 玉溪云南玉溪澄江市機關幼兒園教育集團勞務派遣工作人員招聘7人筆試歷年參考題庫附帶答案詳解
- 湘潭2025年湖南韶山市引進事業(yè)單位急需緊缺專業(yè)人才20人筆試歷年參考題庫附帶答案詳解
- 江西省景德鎮(zhèn)市樂平市樂平中學2024-2025學年高一上學期1月期末英語試題(解析版)
- 江蘇省連云港市2024-2025學年高一(上)期末歷史試卷【含答案】
- 風險管理信托協(xié)議書(2篇)
- 心功能分級護理制定
- 2025屆安徽省“江南十校”高三下學期第一次聯(lián)考(一模)語文試題(教師版)
- 5 《人應當堅持正義》教案統(tǒng)編版選擇性必修中冊
- 2024年江西交通職業(yè)技術學院單招職業(yè)技能測試題庫及答案解析
- GB/T 35714-2017船舶推進電動機
- GB/T 18179-2000金屬覆蓋層孔隙率試驗潮濕硫(硫華)試驗
- GB/T 13024-2003箱紙板
- 導尿管的護理
- 最新國家級課程-頜面骨炎癥的影像診斷課件
- 眼科基礎知識完整版本課件
- 合同的訂立課件
- 軟件編譯環(huán)境問題解決
評論
0/150
提交評論