版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Chapter3:Lists,Stacks,andQueuesThecommentsforExercise3.4regardingtheamountofabstractnessusedapplyhereTherunningtimeoftheprocedureinFig.3.1isO(厶+P)voidPrintLots(ListL.ListP)intCounter;PositionLpos,Ppos;Lpos=First(L);Ppos=First(P);Counter=1;while(Lpos!=NULL&Ppos!=NULL)if(Ppos-Element=Counter+)printf(%
2、?丄posoElement);Ppos=Next(Ppos.P);Lpos=Next(Lpos,L);)Fig.3.1.(a)Forsinglylinkedlists,thecodeisshowninFig.3.2.- -/*BeforePisthecellbeforethetwoadjacentcellsthataretobeswapped.*/*Errorchecksareomittedforclarity.*/voidSwapWithNext(PositionBeforeP,ListL)PositionP,AfterP;P=BeforeP-Next;AfterP=P-Next;/*Bot
3、hPandAfterPassumednotNULL.*/P-Next=AfterP-Next;BeforeP-Next=AfterP:AfterP-Next=P;)Fig.3.2.Fordoublylinkedlists,thecodeisshowninFig.3.3./*PandAfterParecellstobeswitched.Errorchecksasbefore*/voidSwapWithNext(PositionP.ListL)PositionBeforeP.AfterP;BeforeP=P-Prev;AfterP=P-Next:P-Next=AfterP-Next;BeforeP
4、-Next=AfterP:AfterP-Next=P;P-Next-Prev=P:P-Prev=AfterP;AfterP-Prev=BeforeP:)Fig.33Intersectisshownonpage9.TOC o 1-5 h z/*Thiscodecanbemademoreabstractbyusingoperationssuchas*/*RetrieveandIsPastEndtoreplaceLlPos-ElementandLIPos!=NULL*/*Wehaveavoidedthisbecausetheseoperationswerenotrigorouslydefined*/
5、ListIntersect(ListLI,ListL2)ListResult;PositionLIPos丄2P0S9ResultPos;LIPos=First(LI);L2Pos=First(L2);Result=MakeEmpty(NULL);ResultPos=First(Result);while(LIPos!=NULL&L2Pos!=NULL)if(LlPos-ElementElement)LIPos=Next(LIPos,LI);elseif(LIPos-ElementL2Pos-Element)L2Pos=Next(L2Pos,L2);elseInsert(LlPos-Elemen
6、t.Result,ResultPos):LI=Next(LIPos,LI);L2=Next(L2Pos,L2);ResultPos=Next(ResultPos,Result);returnResult;Fig.3.4containsthecodeforUnion.3.7(a)Onealgorithmistokeeptheresultinasorted(byexponent)linkedlist.EachoftheMNmultipliesrequiresasearchofthelinkedlistforduplicatesSincethesizeofthelinkedlistisO(MN).t
7、hetotalrunningtimeisO(M2N2).Theboundcanbeimprovedbymultiplyingonetermbytheentireotherpolynomial,andthenusingtheequivalentoftheprocedureinExercise3.2toinserttheentiresequence.TheneachsequencetakesO(MN),butthereareonlyMofthem,givingatimeboundofO(M2N).AnO(MNlogMN)solutionispossiblebycomputingallMNpairs
8、andthensortingbyexponentusinganyalgorithminChapter7.ItistheneasytomergeduplicatesafterwardThechoiceofalgorithmdependsontherelativevaluesofMandN.Iftheyareclose,thenthesolutioninpart(c)isbetter.Ifonepolynomialisverysmall,thenthesolutioninpart(b)isbetter.ListUnion(ListLI,ListL2)ListResult;ElementTypeIn
9、sertElement;PositionLIPos丄2Pos,ResultPos;LIPos=First(LI);L2Pos=First(L2);Result=MakeEmpty(NULL);ResultPos=First(Result);while(LIPos!=NULL&L2Pos!=NULL)if(LlPos-ElementElement)InsertElement=LlPos-Element;LIPos=Next(LIPos,LI);elseif(LlPos-ElementL2Pos-Element)InsertElement=L2Pos-Element;L2Pos=Next(L2Po
10、s,L2);elseInsertElement=LlPos-Element;LIPos=Next(LIPos,LI);L2Pos=Next(L2Pos,L2);Insert(InsertElement.Result,ResultPos):ResultPos=Next(ResultPos,Result):/*Flushoutremaininglist*/while(LIPos!=NULL)Insert(LlPos-Element,Result,ResultPos);LIPos=Next(LIPos.LI);ResultPos=Next(ResultPos,Result);while(L2Pos!
11、=NULL)Insert(L2Pos-Element,Result,ResultPos);L2Pos=Next(L2Pos.L2);ResultPos=Next(ResultPos,Result);returnResult;)Fig.343.8OnecanusethePowfunctioninChapter2,adaptedforpolynomialmultiplication.IfPissmaltastandardmethodthatusesO(P)multipliesinsteadofO(logP)mightbebetterbecausethemultiplieswouldinvolvea
12、largenumberwithasmallnumber,whichisgoodforthemultiplicationroutineinpart(b).30ThisisastandardprogrammingprojectThealgorithmcanbespedupbysetting=MmodN,sothatthehotpotatonevergoesaroundthecirclemorethanonce,andthenifMN/2.passingthepotatoappropriatelyinthealternativedirection.Thisrequiresadoublylinkedl
13、ist.Theworst-caserunningtimeisclearlyO(Nmin(M,N),althoughwhentheseheuristicsareused,andMandNarecomparable,thealgorithmmightbesignificantlyfaster.IfA/=1,thealgorithmisclearlylinear.TheVAX/VMSCcompilersmemorymanagementroutinesdopoorlywiththeparticularpatternoffreesinthiscase,causingO(NlogN)behavior.32
14、Reversalofasinglylinkedlistcanbedonenonrecursivelybyusingastack,butthisrequiresO(N)extraspaceThesolutioninFig.3.5issimilartostrategiesemployedingarbagecollectionalgorithms.Atthetopofthewhileloop.thelistfromthestarttoviousPosisalreadyreversed,whereastherestofthelist,fromCurrentPostotheendisnormal.Thi
15、salgorithmusesonlyconstantextraspace/*AssumingnoheaderandLisnotempty.*/ListReverseList(ListL)PositionCurrentPos,NextPosPreviousPos;PreviousPos=NULL;CurrentPos=L;NextPos=L-Next;while(NextPos!=NULL)CurrentPos-Next=PreviousPos;PreviousPos=CurrentPos;CurrentPos=NextPos;NextPos=NextPos-Next;CurrentPos-Ne
16、xt=PreviousPos;returnCurrentPos;)Fig.3.5.35(a)ThecodeisshowninFig.3.6.SeeFig.3.7.Thisfollowsfromwell-knownstatisticaltheorems.SeeSleatorandTaijanspaperintheChapter11references36(c)DeletetakesO(N)andisintwonestedforloopseachofsizeN,givinganobviousO(N)boundAbetterboundofO(N_)isobtainedbynotingthatonly
17、NelementscanbedeletedfromalistofsizeN、henceO(N2)isspentperformingdeletes.TheremainderoftheroutineisO(N2).sotheboundfollowsO(N)/*ArrayimplementatioiLstartingatslot1*/PositionFind(ElementTypeX,ListL)intLWhere;Where=0;for(i=1;i1;i)Li.Element=Li-1.Element;Ll.Element=X;return1:elsereturn0:/*Notfound.*/)F
18、ig.3-6.Sortthelist,andmakeascantoremoveduplicates(whichmustnowbeadjacent).3.17(a)Theadvantagesarethatitissimplertocode,andthereisapossiblesavingsifdeletedkeysaresubsequentlyreinserted(inthesameplace)Thedisadvantageisthatitusesmorespace,becauseeachcellneedsanextrabit(whichistypicallyabyte),andunusedc
19、ellsarenotfreedTwostackscanbeimplementedinanarraybyhavingonegrowfromthelowendofthearrayup,andtheotherfromthehighenddown(a)LetEbeourextendedstack.WewillimplementEwithtwostacksOnestack,whichwellcallS,isusedtokeeptrackofthePushandPopoperations,andtheother.M9keepstrackoftheminimum.ToimplementPiish(X,E),
20、weperformPi(sh(X,S)IfXissmallerthanorequaltothetopelementinstackM.thenwealsoperformPush(X.M).ToimplementPop(E),weperformPop(S).IfXisequaltothetopelementinstackM,thenwealsoPop(M).FindMin(E)isperformedbyexaminingthetopofMAlltheseoperationsareclearly0(1).(b)ThisresultfollowsfromatheoreminChapter7thatsh
21、owsthatsortingmusttakeQ(WlogN)time.O(N)operationsintherepertoire,includingDeleteMin,wouldbesufficienttosort./*Assumingaheader.*/PositionFind(ElementTypeX,ListL)PositionPrevPos,XPos;PrevPos=FindPrevious(X丄);if(PrevPos-Next!=NULL)/*Found.*/XPos=PrevPos-Next:PrevPos-Next=XPos-Next;XPos-Next=L-Next;L-Next=XPos;returnXPos;elsereturnNULL:)Fig.3.7.Threestackscanbeimplementedbyhavingonegrowfromthebottomup.another
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025買賣土地合同樣板
- 金屬材料運(yùn)輸保險(xiǎn)合同優(yōu)化
- 2024精裝修南極研究站建設(shè)合同
- 2024裝修合同及預(yù)算
- 2024版購(gòu)銷標(biāo)準(zhǔn)合同范本
- 2024年能源領(lǐng)域招標(biāo)工作計(jì)劃編制合同3篇
- 2024版長(zhǎng)期租車合同
- 2024碎石原料庫(kù)存管理與調(diào)度合同
- 2025年LNG船舶維修與保養(yǎng)服務(wù)合同范本2篇
- 2024虛擬股投資收益分配及稅務(wù)處理合同范本3篇
- GB/T 14040-2007預(yù)應(yīng)力混凝土空心板
- 帶狀皰疹護(hù)理查房課件整理
- 奧氏體型不銹鋼-敏化處理
- 作物栽培學(xué)課件棉花
- 交通信號(hào)控制系統(tǒng)檢驗(yàn)批質(zhì)量驗(yàn)收記錄表
- 弱電施工驗(yàn)收表模板
- 絕對(duì)成交課件
- 探究基坑PC工法組合鋼管樁關(guān)鍵施工技術(shù)
- 國(guó)名、語(yǔ)言、人民、首都英文-及各地區(qū)國(guó)家英文名
- API SPEC 5DP-2020鉆桿規(guī)范
- 組合式塔吊基礎(chǔ)施工專項(xiàng)方案(117頁(yè))
評(píng)論
0/150
提交評(píng)論