【精品数据结构】stack

【精品数据结构】stack

ID:40166629

大小:114.50 KB

页数:23页

时间:2019-07-24

【精品数据结构】stack_第1页
【精品数据结构】stack_第2页
【精品数据结构】stack_第3页
【精品数据结构】stack_第4页
【精品数据结构】stack_第5页
资源描述:

《【精品数据结构】stack》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、Chapter5Stack5.1definitionofstackDefinition:Astackisalinearlistinwhichinsertionsanddeletionstakeplaceatthesameend.Thisendiscalledthetop.Theotherendofthelistiscalledthebottom.ItisalsocalledaLIFO(last-in-first-out)list.5.1definitionofstackFigure5.1stackconfigurationsEtopDtopDCCBBBtopAb

2、ottomAbottomAbottom5.2ADTstackAbstractDataTypeStack{instanceslinearlistofelements;oneendiscalledthebottom;theotheristhetop;operationsCreate():Createanemptystack;IsEmpty():Returntrueifstackisempty,returnfalseotherwise5.2ADTstackIsFull():Returntrueifstackiffull,returnfalseotherwise;Top()

3、:returntopelementofthestack;Add(x):addelementxtothestack;Delete(x):Deletetopelementfromstackandputitinx;}5.3Formula-basedRepresentationWemayusetheFormula-basedrepresentationwhenTop=-1isemptystackstack012topmaxtop-15.3Formula-basedRepresentationFormula-basedclassStacktemplateclass

4、Stack{//LIFOobjectspublic:Stack(intMaxStackSize=10);~Stack(){delete[]stack;}boolIsEmpty()const{returntop==-1;}boolIsFull()const{returntop==MaxTop;}5.3Formula-basedRepresentationTTop()const;Stack&Add(constT&x);Stack&Delete(T&x);Private:inttop;//currenttopofastackintMaxTop;//maxvalue

5、fortopT*stack;//elementarray};5.3Formula-basedRepresentation1)constructorStacktemplateStack::Stack(intMaxStackSize){//stackconstructorMaxTop=MaxStackSize-1;stack=newT[MaxStackSize];top=-1;}5.3Formula-basedRepresentation2)getthetopelementTemplateTStack::Top()const{//

6、returntopelementif(IsEmpty())throwOutOfBounds();elsereturnstack[top];}5.3Formula-basedRepresentation3)pushanelementTemplateStack&Stack::Add(constT&x){//Addxtostackif(IsFull())throwNoMem();stack[++top]=x;return*this;}5.3Formula-basedRepresentation4)popanelementtemplate

7、T>Stack&Stack::Delete(T&x){//deletetopelementandputitinxif(IsEmpty())throwOutOfBounds();x=stack[top--];return*this;}5.3Formula-basedRepresentationItiswastefulofspacewhenmultiplestacksaretocoexistWhenthere’sonlytwostacks,wecanmaintainspaceandtimeefficiencybypeggingth

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。