- 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 树的遍历
4 栈的实现
Remember thatnothing happens when we click the button other thanthe definition of the class. We must create a object and thenuse it. ActiveCode2 showsthe class in action aswe perform the sequence of operations from Table1 .
上面代码仅仅是 stack 类的实现,如果运行的话,什么反应也没有。所以下面的代码,就是创建一个栈对象,并加入操作方法,就如表 1 中所作的操作一样。
s=Stack()
print(s.isEmpty())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())
It isimportant to note that we could have chosen to implement the stackusing a list where the top is at the beginning instead of at theend. In this case, the previous pop and append methods would nolonger work and we would have to index position 0 (the first itemin the list) explicitly using pop and insert. The implementation isshown in CodeLens 1.
注意,我们也可以选择列表的头(左侧)作为栈顶,这样,前面的 pop 和 append 方法就不能用了,而必须指定索引 0(列表的第一个项) 以便对栈内数据操作。如下面代码段:
classStack: def __init__(self): self.items = [] def isEmpty(self): return self.items ==[] def push(self, item): self.items.insert(0,item) def pop(self): returnself.items.pop(0) def peek(self): return self.items[0] def size(self): returnlen(self.items) s = Stack() s.push('hello') s.push('true') print(s.pop())
This ability to change the physical implementationof an abstract data type while maintaining the logicalcharacteristics is an example of abstraction at work. However, eventhough the stack will work either way, if we consider theperformance of the two implementations, there is definitely adifference. Recall that the append and pop() operations were bothO(1). This means that the first implementation will perform pushand pop in constant time no matter how many items are on the stack.The performance of the second implementation suffers in that theinsert(0) and pop(0) operations will both require O(n) for a stackof size n. Clearly, even though the implementations are logicallyequivalent, they would have very different timings when performingbenchmark testing.
对抽象数据类型的实现方式的变更,仍能保持数据的逻辑特性不变,就是“抽象”的实例。两种栈的方式都能工作,但性能表现却有很大的不同。Append() 和 pop() 都是 O(1),这意味着,不管栈内有多少数据项,第一种实现的性能是常数级的,第二种实现的 insert(0) 和 pop(0) 却需要 O(n)。很明显,逻辑上是等同的,但在性能基准测试时,时间测试的结果是非常之不同的。
Self Check
自测题
stk-1: Given the following sequence of stack operations,what is the top item on the stack when the sequence iscomplete?
经过以下操作后,栈顶数据项是哪个?
m = Stack()
m.push('x')
m.push('y')
m.pop()
m.push('z')
m.peek()
stk-2: Given the following sequence of stack operations,what is the top item on the stack when the sequence iscomplete?
第二题:经以下测试,栈顶元素是哪个?
m = Stack()
m.push('x')
m.push('y')
m.push('z')
whilenot m.isEmpty():
m.pop()
m.pop()
Write afunction revstring(mystr) thatuses a stack to reverse the characters in a string.
第三题,为以下代码补充一个函数 revstring(mystr),实现将一个字符串翻转顺序。
from testimport testEqual
frompythonds.basic.stack import Stack
defrevstring(mystr):
#在此处补充代码
testEqual(revstring('apple'),'elppa')
testEqual(revstring('x'),'x')
testEqual(revstring('1234567890'),'0987654321')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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