基本数据结构StacksQueuesListsTrees.ppt

基本数据结构StacksQueuesListsTrees.ppt

  1. 1、本文档共28页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 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

文档评论(0)

duoduoyun + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

认证主体黄**

1亿VIP精品文档

相关文档

相关课程推荐