




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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)鏈表的長(zhǎng)度"< cout<<" 7-雙向循環(huán)鏈表清空&qu
11、ot;< cout<<" 0-退出 "< cout<<"*"< cout<<"請(qǐng)輸出功能序號(hào):"#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-雙鏈表頭部插入一個(gè)元素"< cout<<"2-雙鏈表尾部插入一個(gè)元素"< cout<<"3-雙鏈表任意位置插入元素"< cout<<"*"< cout<<"請(qǐng)選擇:"cin>>n;if(n=1cout<<"請(qǐng)輸入頭部要插入的元素值:"cin>>x;dlist.HeadInsert(x;else if(n=2cout<<"
13、請(qǐng)輸入尾部要插入的元素值:"cin>>x;dlist.TailInsert(x;elsecout<<"請(qǐng)輸入位序和元素值:"cin>>pos>>x;dlist.Insert(pos,x;break; break;case 3:cout<<"請(qǐng)輸入要?jiǎng)h除的結(jié)點(diǎn)位序:"cin>>pos;dlist.DeleteNode(pos,x;break;case 4:cout<<"*"< cout<<"1-按元素的數(shù)值查找&qu
14、ot;< cout<<"2-按元素的位置查找"< cout<<"*"< cout<<"請(qǐng)選擇:"cin>>n;if(n=1cout<<"請(qǐng)輸入要查找的元素值:"cin>>x;cout<<"位置為:"< elsecout<<"請(qǐng)輸入所需查找元素的位置:"cin>>pos;DblNode *p=dlist.GetNode(pos;if(pcout<<"第"< " 個(gè)元素值為: " < data< break;case 5:cout<<"請(qǐng)輸入要改變結(jié)點(diǎn)的位序和改制后
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 個(gè)人裝修委托書(shū)
- 雙方設(shè)備合作合同范本
- 南瓜訂貨合同范例
- 斷層解剖復(fù)習(xí)題+答案
- 合伙買(mǎi)車(chē)合作協(xié)議合同范例
- 七年級(jí)下學(xué)期語(yǔ)文總結(jié)
- 兼職游泳教練合同范本
- 保潔合同范本(完美版)
- 廠里員工租房合同范本
- 《贈(zèng)劉景文》和《山行》古詩(shī)的教學(xué)反思
- 2025年萍鄉(xiāng)衛(wèi)生職業(yè)學(xué)院?jiǎn)握新殬I(yè)傾向性測(cè)試題庫(kù)審定版
- 人教版四年級(jí)數(shù)學(xué)下冊(cè)《圖形的運(yùn)動(dòng)(二)》試題(含答案)
- 2024-2025學(xué)年五年級(jí)(下)信息科技教學(xué)計(jì)劃
- 《老年人權(quán)益保障法》
- 2024年江西交通職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫(kù)及答案解析
- 人衛(wèi)版急診與災(zāi)難醫(yī)學(xué)之呼吸困難教學(xué)課件
- word公章模板
- 中西醫(yī)結(jié)合腫瘤學(xué)試卷(含答案)
- 開(kāi)學(xué)第一課我們開(kāi)學(xué)啦主題班會(huì)PPT課件(帶內(nèi)容)
- 體育訓(xùn)練隊(duì)隊(duì)規(guī)
- 電梯工程開(kāi)工報(bào)告(直梯)(共1頁(yè))
評(píng)論
0/150
提交評(píng)論