资源描述:
《数据结构与算法分析 C语言描述(第2版)Larry Nyhoff 栈 课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、StacksChapter71Nyhoff,ADTs,DataStructuresandProblemSolvingwithC++,SecondEdition,©2005PearsonEducation,Inc.Allrightsreserved.0-13-140909-3SelectingStorageStructureModelwithanarrayLetposition0betopofstackProblem…considerpushingandpopping2Nyhoff,ADTs,DataStructuresan
2、dProblemSolvingwithC++,SecondEdition,©2005PearsonEducation,Inc.Allrightsreserved.0-13-140909-3SelectingStorageStructureAbetterapproachistoletposition0bethebottomofthestackThusourdesignwillincludeAnarraytoholdthestackelementsAnintegertoindicatethetopofthestackNoteb
3、eginningofStack.hfile,Fig.7.33Nyhoff,ADTs,DataStructuresandProblemSolvingwithC++,SecondEdition,©2005PearsonEducation,Inc.Allrightsreserved.0-13-140909-3ImplementingOperationsConstructorCompilerwillhandleallocationofmemoryEmptyCheckifvalueofmyTop==-1Push(ifmyArrayn
4、otfull)IncrementmyTopby1StorevalueinmyArray[myTop]TopIfstacknotempty,returnmyArray[myTop]PopIfarraynotempty,decrementmyTopOutputroutineaddedfortesting4Nyhoff,ADTs,DataStructuresandProblemSolvingwithC++,SecondEdition,©2005PearsonEducation,Inc.Allrightsreserved.0-13
5、-140909-3TheStackClassThecompletedStack.hfile,Fig.7.4AAllfunctionsdefinedNoteuseoftypedefmechanismImplementationfile,Stack.cpp,Fig7.4BDriverprogramtotesttheclass,Fig7.5Createsstackof4elementsDemonstrateserrorcheckingforstackfull,empty5Nyhoff,ADTs,DataStructuresand
6、ProblemSolvingwithC++,SecondEdition,©2005PearsonEducation,Inc.Allrightsreserved.0-13-140909-3DynamicArraytoStoreStackElementsSameissuesregardingstaticarraysforstacksasforlistsCanrunoutofspaceifstacksettoosmallCanwastespaceifstacksettoolargeAsbefore,wedemonstratead
7、ynamicarrayimplementationtosolvetheproblemsNoteadditionaldatamembersrequiredDStackDataMembers6Nyhoff,ADTs,DataStructuresandProblemSolvingwithC++,SecondEdition,©2005PearsonEducation,Inc.Allrightsreserved.0-13-140909-3DynamicArraytoStoreStackElementsConstructormustC
8、heckthatspecifiednumElements>0SetcapacitytonumElementsAllocateanarraypointedtobymyArraywithcapacity=myCapacitySetmyTopto-1ifallocationgoesOKNoteimplemen