版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
數(shù)據(jù)結(jié)構(gòu)與程序設(shè)計(jì)(16)王麗蘋lipingwang@4/21/20231.ImplementationofLinkedList2BookP225改進(jìn)的思路KeepingSupposeanapplicationprocesseslistentriesinorderorreferstothesameentryseveraltimesbeforeprocessinganotherentry.thelast-usedPositionRememberthelast-usedpositioninthelistand,ifthenextoperationreferstothesameoralaterposition,starttracingthroughthelistfromthislast-usedposition.4/21/20232.ImplementationofLinkedList2template<classList_entry>classList{public: ~List(); List(); List(constList<List_entry>©); voidoperator=(constList<List_entry>©); intsize()const; boolfull()const; boolempty()const; voidclear(); voidtraverse(void(*visit)(List_entry&)); Error_coderetrieve(intposition,List_entry&x)const; Error_codereplace(intposition,constList_entry&x); Error_coderemove(intposition,List_entry&x); Error_codeinsert(intposition,constList_entry&x);4/21/20233.ImplementationofLinkedList2protected://Datamembersforthelinkedlist//implementationnowfollow. intcount; mutableintcurrent_position; mutableNode<List_entry>*current; Node<List_entry>*head;//Thefollowingauxiliaryfunctionisusedto//locatelistpositions voidset_position(intposition)const;};4/21/20234.C++中的關(guān)鍵字:mutable關(guān)鍵字mutable的含義:類的mutable成員能夠被任何成員函數(shù)修改,即使是const修飾的常量成員函數(shù)也能夠?qū)λM(jìn)行修改。4/21/20235.ImplementationofLinkedList2List<List_entry>::List(){ count=0; head=NULL; current_position=0; current=NULL;}4/21/20236.ImplementationofLinkedList2template<classList_entry>voidList<List_entry>::set_position(intposition)const/*Pre:positionisavalidpositionintheList:0<=position<count.Post:ThecurrentNodepointerreferencestheNodeatposition.*/{ if(position<current_position){//muststartoveratheadoflist current_position=0; current=head; } for(;current_position!=position;current_position++) current=current->next;}4/21/20237.ImplementationofLinkedList2template<classList_entry>Error_codeList<List_entry>::insert(intposition,constList_entry&x){ if(position<0||position>count) returnrange_error; Node<List_entry>*new_node,*previous,*following; if(position>0){
set_position(position-1); previous=current; following=previous->next; } elsefollowing=head; new_node=newNode<List_entry>(x,following); if(new_node==NULL) returnoverflow;4/21/20238.ImplementationofLinkedList2 if(position==0){ head=new_node; //shouldbeadded current_position=0; current=head; } else{ previous->next=new_node; //shouldbeadded set_position(position); } count++; returnsuccess;}4/21/20239.ImplementationofLinkedList2
position-1 positionpreviousfollowingPosition>0Position=0followingheadnew_nodenew_node4/21/202310.ImplementationofLinkedList2template<classList_entry>Error_codeList<List_entry>::remove(intposition,List_entry&x){ if(position<0||position>=count) returnrange_error; Node<List_entry>*previous,*following; if(position>0){
set_position(position-1); previous=current; following=previous->next; previous->next=following->next; }4/21/202311.ImplementationofLinkedList2 else{ following=head; head=head->next; //shouldbeadded current_position=0; current=head; }x=following->entry; deletefollowing; count--; returnsuccess;}4/21/202312.ImplementationofLinkedList2 position-1 positionpreviousfollowingPosition>0Position=0Position=0followinghead4/21/202313.ImplementationofLinkedList2template<classList_entry>Error_codeList<List_entry>::retrieve(intposition,List_entry&x)const{ if(position<0||position>=count) returnrange_error; Node<List_entry>*p_node; set_position(position); x=current->entry; returnsuccess;}4/21/202314.ImplementationofLinkedList2目錄LinkList2下例程上機(jī)完成LinkList24/21/202315.進(jìn)一步的改進(jìn)Keepingthelast-usedPosition當(dāng)插入的位置在最后一次使用的位置之后時(shí),效率較高。當(dāng)插入的位置在最后一次使用的位置之前時(shí),需要從鏈表頭部重新計(jì)數(shù)。改進(jìn)方法:將單鏈表變?yōu)殡p鏈表4/21/202316.DoublyLinkedListsBOOKP227figure6.34/21/202317.DoublyLinkedLists//定義雙鏈表節(jié)點(diǎn)類型template<classNode_entry>structNode{//datamembers Node_entryentry; Node<Node_entry>*next; Node<Node_entry>*back;//constructors Node(); Node(Node_entryitem,Node<Node_entry>*link_back=NULL, Node<Node_entry>*link_next=NULL);};4/21/202318.DoublyLinkedListstemplate<classList_entry>voidList<List_entry>::set_position(intposition)const/*Pre:positionisavalidpositionintheList:0<=position<count.Post:ThecurrentNodepointerreferencestheNodeatposition.*/{ if(current_position<=position) for(;current_position!=position;current_position++) current=current->next; else for(;current_position!=position;current_position--) current=current->back;}4/21/202319.DoublyLinkedLists4/21/202320.DoublyLinkedLists(BOOKP229figure6.4)
Error_codeList<List_entry>::insert(intposition,constList_entry&x){ Node<List_entry>*new_node,*following,*preceding; if(position<0||position>count)returnrange_error; if(position==0){//在第0號(hào)位置插入的特殊情況 if(count==0)following=NULL; else{ set_position(0); following=current; } preceding=NULL; } else{//一般情況,在鏈表中間或者末尾插入 set_position(position-1); preceding=current; following=preceding->next; }//給following和preceding指針賦初始值。4/21/202321.DoublyLinkedLists(BOOKP229figure6.4)
new_node=newNode<List_entry>(x,preceding,following);//產(chǎn)生新節(jié)點(diǎn) if(new_node==NULL)returnoverflow; if(preceding!=NULL)preceding->next=new_node; if(following!=NULL)following->back=new_node; //調(diào)整current和current_position指針current=new_node; current_position=position;
//Shouldbeadded. if(position==0)head=new_node; count++; returnsuccess;}4/21/202322.DoublyLinkedLists
position-1 positionprecedingfollowingPosition>0Position=0Count!=0followingheadnew_nodenew_nodenew_nodePosition=0Count=04/21/202323.DoublyLinkedListsError_codeList<List_entry>::remove(intposition,List_entry&x){ if(position<0||position>=count) returnrange_error; Node<List_entry>*previous,*following; if(position>0){ set_position(position-1); previous=current; following=previous->next; previous->next=following->next; if(following->next)following->next->back=previous; }4/21/202324.DoublyLinkedLists else{ following=head; head=head->next; if(head)head->back=NULL; //shouldbeadded current_position=0; current=head; }x=following->entry; deletefollowing; count--; returnsuccess;}4/21/202325.DoublyLinkedLists position-1 positionpreviousfollowingPosition>0Position=0Position=0followinghead4/21/202326.DoublyLinkedListstemplate<classList_entry>classList{public: ~List(); List(); List(constList<List_entry>©); voidoperator=(constList<List_entry>©); intsize()const; boolfull()const; boolempty()const; voidclear(); voidtraverse(void(*visit)(List_entry&)); Error_coderetrieve(intposition,List_entry&x)const; Error_codereplace(intposition,constList_entry&x); Error_coderemove(intposition,List_entry&x); Error_codeinsert(intposition,constList_entry&x);4/21/202327.DoublyLinkedListsprotected://Datamembersforthelinkedlistimplementationnowfollow. intcount; mutableintcurrent_position; mutableNode<List_entry>*current; Node<List_entry>*head;//Thefollowingauxiliaryfunctionisusedt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(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)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版?zhèn)€人車輛抵押債權(quán)債務(wù)處理專項(xiàng)協(xié)議4篇
- 二零二五年度房產(chǎn)置換及配套設(shè)施建設(shè)協(xié)議3篇
- 二零二五年度錨桿施工與地質(zhì)災(zāi)害防治合同4篇
- 二零二五年度出租車租賃與城市交通規(guī)劃合同4篇
- 個(gè)人二手房交易法律合同版
- 2025年度配電箱智能化改造項(xiàng)目合同4篇
- 2025年度個(gè)人之間房屋買賣稅費(fèi)承擔(dān)合同范本3篇
- 二零二五版智能代賬系統(tǒng)應(yīng)用服務(wù)合同2篇
- 2025年度鋁合金汽車零部件研發(fā)采購(gòu)合同3篇
- 2025年護(hù)理院護(hù)理團(tuán)隊(duì)建設(shè)與管理合同3篇
- 小兒甲型流感護(hù)理查房
- 霧化吸入療法合理用藥專家共識(shí)(2024版)解讀
- 2021年全國(guó)高考物理真題試卷及解析(全國(guó)已卷)
- 拆遷評(píng)估機(jī)構(gòu)選定方案
- 趣味知識(shí)問答100道
- 鋼管豎向承載力表
- 2024年新北師大版八年級(jí)上冊(cè)物理全冊(cè)教學(xué)課件(新版教材)
- 人教版數(shù)學(xué)四年級(jí)下冊(cè)核心素養(yǎng)目標(biāo)全冊(cè)教學(xué)設(shè)計(jì)
- JJG 692-2010無創(chuàng)自動(dòng)測(cè)量血壓計(jì)
- 三年級(jí)下冊(cè)口算天天100題(A4打印版)
- CSSD職業(yè)暴露與防護(hù)
評(píng)論
0/150
提交評(píng)論