Stacks

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

3STACKS3.1INTRODUCTIONAstackisalineardatastructure.Itisveryusefulinmanyapplicationsofcomputerscience.Itisalistinwhichallin-sertionsanddeletionsaremadeatoneend,calledthetopofthestack.Someoftheanalogiesusedtovisualizethisdatastructurearea)StackofTrays(or)PlatesPlacedonaTableHere,oneplateisplacedontopofanother,thuscreatingastackofplates.Suppose,apersontakesaplateoffthetopofthestackofplates.Theplatemostrecentlyplacedonthestackisthefirstonetobetakenoff.Thebottomplateisthefirstoneplacedonthestackandthelastonetoberemoved.b)AnotherFamiliarExampleofaStackisaRailwayStationforShuntingCarsInthisexample,thelastrailwaycarplacedonthestackisthefirsttoleave.c)ShipmentinaCargoFortheshipmentofgoods,theyhavetobeloadedintoacargo.Duringunloading,theyareunloadedexactlyintheoppositeorderastheyareloaded,thatis,thegoodswhichareloadedlastshouldbeunloadedfirst.3.2DEFINITIONAstackisanorderedcollectionofhomogeneousdataelementswheretheinsertionanddeletionoperationsoccuratoneendonly,calledthetopofthestack.Fig.3.3showstheschematicdiagramofastack.ThealternativerepresentationofastackisgiveninFigure.3.4.Othernamesforastackarepushdownlist,andLIFO(or)LastInFirstOutlist.Thestackallowsaccesstoonlyoneitem,i.e.,thelastiteminserted.Intheschematicdia-gramofastack,afterinsertingItem4intothestack,itactsasthetopmostelement.Itwillberemovedfirst.TheveryfirstitemthatwasinsertedintothestackisItem1.Itwillberemovedasthelastitem.Fig.3.1StackofplatesCargoStackFig.3.2Arailwayshuntingsystem—represenationofastack69Stacks3.3PRIMITIVEOPERATIONSTheprimitiveoperationsthatcanbeperformedonastackaregivenbelow:1.Insertinganelementintothestack(PUSHoperation)2.Removinganelementfromthestack(POPoperation)3.Determiningthetopitemofastackwithoutremovingitfromthestack(PEEPoperation)ThesyntaxusedforthePUSHoperationisPUSH(stack,item)wherestackisthenameofthestackintowhichtheitemspecifiedasthesecondargumentisplacedatthetoppositionofthestack.ThesyntaxusedforthePOPoperationisPOP(stack)wherestackisthenameofthestackfromwhichtheitemhastoberemoved,i.e.,theelementatthetopmostpositionofthestackisremoved.ConsiderastackstructureasgiveninFigure3.5(a).Afterinserting(PUSH)anelement(viz,55)intothisstack,thecontentsofthestackafterthePUSHoperationisgiveninFigure3.5(b).SupposeifthePOPoperationisspecifiedasPOP(st).ThenthecontentofthestackcanbevisualizedasinFigure3.5(c).PushPopTopItem4Item3Item2Item1BottomFig.3.3SchematicdiagramofastackBottomTopFig.3.4AlternativerepresentationofastackTop443322Fig.3.5(a)Fig.3.5(b)Fig.3.5(c)Top443322Thatis,theelementpointedoutbyTopwillberemovedandthenTopwillbedecrementedby1.Soitpointstheitemplacedbelowtheremoveditem.Itisalsopos-sibletoverifytheitemplacedatthetopofthestackwithoutremovingit.Thisopera-tioniscalledPEEP.ThesyntaxusedforthisoperationisTop4433225570DataStructuresUsingCPEEP(st)ThisoperationisthecombinationofPOPandPUSH.Itisequivalenttothefollow-ingstatements:i=POP(st)PUSH(st,i)POPremovestheitemfromthepositionpointedoutbyTop.Itisinvariablei.ThenusingthePUSHinstructionweareinsertingthesameitemiintothestack.Nowthevariableihasthevalueinthetopmostpositionofthestack.3.3.1OtherOperationsTheotheroperationsthatcanbeperformedonthestackarethefollowing:1)CheckingtheEmptinessoftheStackItisnotpossibletopopelementsfromtheemptystack.SoatthetimeofissuingthePOPoperation,thestackshouldbenon-empty.ThesyntaxusedtochecktheemptyconditionofthestackisEmpty(stack)ItreturnsTRUEifthestackisemptyandFALSEotherwise.2)CheckingtheFullConditionoftheStackIfthestackisfull,itisnotpossibletopushelementsintoit.SobeforeissuingthePUSHoperation,makesurethatthestackisnotfull.ThesyntaxusedtocheckthefullconditionofthestackisFull(stack)ItreturnsTRUEifthestackisFullandFALSEotherwise.3)BeforeUsingtheStack,theSizeoftheStackandtheVariablewhichPointstheTopElementoftheStackhavetobeInitialized3.4ANABSTRACTDATATYPE(ADT)AnAbstractDataType(ADT)isakeywordusedinaprogramminglanguagetospecifytheamountofmemoryneededtostoredataandthetypeofdatathatwillbestoredinthatmemorylocation.However,anADTdoesnotspecifyhowmanybytestoreserveforthedata,becausethenumberofbytesreservedforanADTvariesfromoneprogramminglanguagetoanother.ADTforStackThisisaLast-InFirst-Outstructure.Itemscanonlybeinsertedandremovedfromthetopofthestack.TheADTforstackarethefollowing:1)Push()(or)Put(or)insertnewitemontopofstack2)Pop()removeandreturntopitemofstack3)Top()Verifiesthetopitemofstack3.5IMPLEMENTATIONAstackcanbeimplementedinanyoneofthefollowingtwoways:1.Usinganarray2.Usingalinkedlist71Stacks3.5.1ArrayImplementationofStacksInthearrayimplementationofastack,astackcangroweithertowardsthehigh-indexedendofthearray(or)towardsthelow-indexedendofthearray.ThearrayimplementationofthestackwhichgrowstowardsthelowindexedendofthearrayisshowninFig.3.6.Thearrayimplementationrequiresthefollowing.1)Anarray2)AvariabletoptoholdthetopmostelementofthestackHere,thestackisoffixedsize.Thismeansthatsomemaximumlimitisspecifiedforstoringtheelementsintothestack.Oncethemaximumlimitisreached,itisnotpossibletostoretheelementsintoit.InFigure3.6,thelow-indexedendofthearrayholdsthelastelement

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功