python数据结构学习笔记-4-堆栈 队列
堆栈
堆栈是一个对象的集合,往集合中增加对象和从集合中移除对象必须符合后进先出原则(last-in, first-out() principle) 。在python中用list实现堆栈很方便,只要按照堆栈抽象数据类型做一个转换就可以了。
#堆栈
class Empty(Exception):
"""Error attempting to access an element from an empty container."""
pass
class ArrayStack:
"""LIFO Stack implementation using a Python list as underlying storage."""
def __init__(self):
"""Create an empty stack."""
self._data = []
def __len__(self):
"""Return the number of elements in teh stack."""
return len(self._data)
def is_empty(self):
"""Return True if the stack is empty."""
return len(self._data) == 0
def push(self, e):
"""Add element to the top of the stack."""
self._data.append(e)
def top(self):
"""Return (but do not remove) the element at the top of the stack.
Raise Empty exception if the stack is empty.
"""
if self.is_empty():
raise Empty('Stack is empty')
return self._data[-1]
def pop(self):
"""Remove and return the element from the
Tisfy: 在看到这篇文章之前,我对人世间是否有真正的圣人是怀疑的;而现在,我终于相信了!
Jozky86: 非常感谢你的代码,我终于做出第二阶段,哭了哭了
weixin_45225467: 挺好的
gaohailong1 回复 非正经研究生: plt.show()没写吧
hezhaoxin1202: 你好,博主,数据集能分享一下吗,我想比对着做个我自己的数据集的试验