- 1、本文档共28页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
基本数据结构StacksQueuesListsTrees
Elementary Data Structures2The Stack ADT (§2.1.1)The Stack ADT stores arbitrary objectsInsertions and deletions follow the last-in first-out schemeThink of a spring-loaded plate dispenserMain stack operations:push(object): inserts an elementobject pop(): removes and returns the last inserted elementAuxiliary stack operations:object top(): returns the last inserted element without removing itinteger size(): returns the number of elements storedboolean isEmpty(): indicates whether no elements are stored
Elementary Data Structures3Applications of StacksDirect applicationsvisited history in a Web browserUndo sequence in a text editorChain of method calls in the Java Virtual Machine or C++ runtime environmentIndirect applicationsAuxiliary data structure for algorithmsComponent of other data structures
Elementary Data Structures4Array-based Stack (§2.1.1)A simple way of implementing the Stack ADT uses an arrayWe add elements from left to rightA variable t keeps track of the index of the top element (size is t+1)S012t…Algorithm pop(): if isEmpty() then throw EmptyStackException else t ? t ? 1 return S[t + 1]Algorithm push(o) if t = S.length ? 1 then throw FullStackException else t ? t + 1 S[t] ? o
Elementary Data Structures5Growable Array-based Stack (§1.5)In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger oneHow large should the new array be?incremental strategy: increase the size by a constant cdoubling strategy: double the sizeAlgorithm push(o) if t = S.length ? 1 then A ? new array of size … for i ? 0 to t do A[i] ? S[i] S ? A t ? t + 1 S[t] ? o
Elementary Data Structures6Comparison of the StrategiesWe compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operationsWe assume that we start with an empty stack represented by an array of size 1We call amortized time of a push oper
您可能关注的文档
最近下载
- 2024首届全国红旗杯班组长大赛题库及答案 (1一2000题).docx VIP
- 转体桥梁要点施工方案(精品路局审查通过版).docx
- 【小学】教育教学知识与能力-教学设计题 (第四章第五章)(答题模板与技巧完整版).ppt VIP
- 基于“双减”政策下的初中英语作业设计新思维优秀获奖科研论文.docx
- 安徽省合肥市瑶海区2022-2023学年下学期七年级期末数学试卷(含答案) .docx
- 2023年淮南一中自主招生综合测试试卷.doc
- 初中英语外研社课标版七年级下册(2011)M11U1Body Language 公开课.pptx
- 《红楼梦》中的服饰文化.ppt
- PEMS开发生命周期-GB9706.docx
- 中国艺术史智慧树知到答案章节测试2023年四川音乐学院 .pdf VIP
文档评论(0)