- 1 基本数据结构
- 2 栈的概念
- 3 栈的抽象数据类型
- 4 栈的实现
- 5 栈的应用之圆括号平衡
- 6 栈的应用之符号平衡(通用)
- 7 栈的应用之进制转换
- 8 栈的应用之中缀前缀后缀
- 9 中缀后前缀、后缀的转换思路
- 10 栈的应用之中缀转后缀表达式算法的实现
- 11 后缀表达式求值
- 12 队列的概念
- 13 队列的抽象数据类型
- 14 队列的 python 实现
- 15 队列应用之烫手的山芋
- 16 队列应用之 打印任务
- 17 列表
- 18 无序列表的实现
- 19 有序列表 ADT 及实现
- 20 递归和递归三定律
- 21 递归的实现和应用
- 22 递归图形
- 23 宾斯基三角形
- 24 汉诺塔问题(河内塔问题)
- 25 探索迷宫
- 26 动态规划
- 27 排序与查找 顺序查找
- 28 二分查找
- 30 冒泡排序
- 31 选择排序
- 29-1 哈希查找
- 29-2 冲突解决
- 29-3 用哈希表实现映射
- 32 插入排序
- 33 希尔排序
- 34 归并排序
- 35 快速排序
- 36 树的基本概念
- 37 树的实现
- 38 分析树
- 39 树的遍历
2 栈的概念
Stacks 栈
What is a Stack? 什么是栈?
A stack (sometimescalled a “ push-down stack ”) is an orderedcollection of items where the addition of new items and the removalof existing items always takes place at the same end. This end iscommonly referred to as the “top.” The end opposite the top isknown as the “base.”
栈(也叫下推栈)一种线性有序的数据元素集合,它的特点是,数据的增加删除操作都在同一端进行。进行操作的这一端,我们一般叫做“顶”,另一端叫做“底”。
The base of the stack is significant since items stored in thestack that are closer to the base represent those that have been inthe stack the longest. The most recently added item is the one thatis in position to be removed first. This ordering principle issometimes called LIFO, last-infirst-out. It provides an ordering based on lengthof time in the collection. Newer items are near the top, whileolder items are near the base.
栈的底部很有象征性,因为元素越接近底部,就意味着在栈里的时间越长。最近进来的,总是最早被移走,这种排列规律叫做先进后出,综合为 LIFO。所以栈的排序是按时间长短来排列元素的。新来的在栈顶,老家伙们在栈底(译者注:中国有句成语,叫后来居上。原典故是这样说的:陛下用群臣,如积薪耳,后来者居上。栈的方式,可不就是堆柴草吗?)
Many examples of stacks occur in everyday situations. Almost anycafeteria has a stack of trays or plates where you take the one atthe top, uncovering a new tray or plate for the next customer inline. Imagine a stack of books on a desk ( Figure1 ). The only book whose cover is visible is the oneon top. To access others in the stack, we need to remove the onesthat are sitting on top ofthem. Figure2 shows another stack. This one contains a number ofprimitive Python data objects.
栈的例子经常看到。比如自助餐厅的盘,人们总是从上面拿盘子,拿走一个后面的人再拿下面的一个,(服务员端来一些新的,又堆在上面了)。又如一堆书,(图 1)你只能看到最上面一本的封面,要看下面一本,就要把上面的先拿 走。图 2 是另一种栈,存储的是几个主要的 python 语言数据对象。
Figure 1: A Stack of Books
Figure 2: A Stack of Primitive Python Objects
One of the most useful ideas related to stacks comes from thesimple observation of items as they are added and then removed.Assume you start out with a clean desktop. Now place books one at atime on top of each other. You are constructing a stack. Considerwhat happens when you begin removing books. The order that they areremoved is exactly the reverse of the order that they were placed.Stacks are fundamentally important, as they can be used to reversethe order of items. The order of insertion is the reverse of theorder of removal. Figure3 shows the Pythondata object stack as it was created and then again as items areremoved. Note the order of the objects.
与栈有关的思想来自生活中的观察,假设你从一张干净的桌子开始,一本一本地放上书,这就是在建立栈。当你一本一本地拿走,想像一下,是不是先进后出?由于这种结构具有翻转顺序的作用,所以非常重要。图 3 显示了 Python 数据栈,加入和移走的顺序。
Figure 3: The Reversal Property of Stacks
Considering this reversal property, you can perhaps think ofexamples of stacks that occur as you use your computer. Forexample, every web browser has a Back button. As you navigate fromweb page to web page, those pages are placed on a stack (actuallyit is the URLs that are going on the stack). The current page thatyou are viewing is on the top and the first page you looked at isat the base. If you click on the Back button, you begin to move inreverse order through the pages.
栈这种翻转性,在你用电脑上网的时候也用到了。浏览器软件上都有“返回”按钮,当你从一个链接到另一个链接,这时网址(URL)就被存进了栈。正在浏览的页就存在栈顶,点“返回”的时候,返回到刚刚浏览的页面。最早浏览的页面,要一直到最后才能看到。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论