[Python数据结构] 使用List实现Stack
[Python数据结构] 使用List实现Stack
1. Stack
堆栈(Stack)又称为栈或堆叠,是计算机科学中一种特殊的串列形式的抽象数据类型(ADT),其特殊之处在于只能允许在阵列的一端进行加入数据和删除数据,并且执行顺序应按照后进先出(LIFO)的原则。
堆栈[维基百科]
2. Stack ADT
堆栈是一种抽象数据类型,其实例S需要支持两种方法:
1)S.push(e) : add element e to the top of stack S
2)S.pop( ) : remove and return the top element from the stack S
另外,为了方便使用,我们还定义了以下方法:
3)S.top() : return the top element from the stack S, but not remove it
4)S.is_empty() : Return True if the stack is empty
5)len(S) : Return the number of elements in the stack
3. Implementing a Stack using a List
class Empty(Exception):
"""Error attempting to access an element from an empty container"""
pass class OverFlow(Exception):
"""Error attempting to push an element to an full container"""
pass
class ArrayStack():
"""LIFO Stack implementation using a Python list as underlying storage""" def __init__(self, n):
"""Create an empty stack."""
self.data = []
self.maxLen = n # n : an integer that represent the max elements capacity of the stack def __len__(self):
"""Return the number of elements in the stack"""
return len(self.data) def is_empty(self):
"""Return True if the stack is empty"""
return len(self.data) == 0 def is_full(self):
"""Return True if the stack is full"""
return len(self.data) == self.maxLen def push(self, e):
"""Add element e to the top of the stack Raise Empty exception if the stack is full"""
if self.is_full():
raise OverFlow('Stack is full')
return self.data.append(e) def top(self):
"""Return the element at the top of the stack, but not move it. Raise Empty exception if the stack is empty"""
if self.is_empty():
raise Empty('Stack is empty')
return self.data[-1] def pop(self):
"""Return the element at the top of the stack, meanwhile move it. Raise Empty exception if the stack is empty"""
if self.is_empty():
raise Empty('Stack is empty')
return self.data.pop()
4. 执行结果:
s = ArrayStack(10)
l = np.random.randint(0, 10, size=(10, ))
for i in l:
s.push(i)
s.data
[6, 8, 7, 4, 2, 3, 4, 1, 5, 8]
s.pop()
8
s.data
[6, 8, 7, 4, 2, 3, 4, 1, 5]
s.top()
5
s.data
[6, 8, 7, 4, 2, 3, 4, 1, 5]
[Python数据结构] 使用List实现Stack的更多相关文章
- python数据结构之栈与队列
python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...
- Python - 数据结构 - 第十五天
Python 数据结构 本章节我们主要结合前面所学的知识点来介绍Python数据结构. 列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和 ...
- Python数据结构汇总
Python数据结构汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.线性数据结构 1>.列表(List) 在内存空间中是连续地址,查询速度快,修改也快,但不利于频繁新 ...
- python数据结构之二叉树的统计与转换实例
python数据结构之二叉树的统计与转换实例 这篇文章主要介绍了python数据结构之二叉树的统计与转换实例,例如统计二叉树的叶子.分支节点,以及二叉树的左右两树互换等,需要的朋友可以参考下 一.获取 ...
- Python数据结构与算法之图的广度优先与深度优先搜索算法示例
本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法.分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 ...
- python数据结构与算法
最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- python数据结构之图的实现
python数据结构之图的实现,官方有一篇文章介绍,http://www.python.org/doc/essays/graphs.html 下面简要的介绍下: 比如有这么一张图: A -> B ...
- Python数据结构与算法--List和Dictionaries
Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
随机推荐
- Swing中子元素截获MouseEvent问题
在父元素中绑定MouseMotion监听,但是当鼠标在子元素中时父元素无法收到 这时候需要在子元素中绑定MouseMotion,然后使用: getParent().dispatchEvent(e); ...
- bzoj2709: [Violet 1]迷宫花园
二分答案,spfa check就行了. gb题卡精度. #include<cstdio> #include<iostream> #include<cstring> ...
- 怎么查询数据库中第30到40条记录呢? 通过ID,查询当前第30-40条记录 注意,ID不是顺序的
http://blog.csdn.net/lee576/article/details/5812347 http://bbs.csdn.net/topics/190070614 http://www. ...
- div标签的闭合检查
什么叫DIV标签有没有闭合呢?有<div>开头就应该有</div>来结尾闭合了.有时候写代码写 了<div>,忘记</div>结尾,谓之没有闭合也. 如 ...
- springAOP配置XML方式配置切面报错error at ::0 formal unbound in pointcut
[错误配置文件] <aop:config> <aop:pointcut expression="execution(* net.fifteenho.service.impl ...
- 最优贸易 2009年NOIP全国联赛提高组(最短路)
最优贸易 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description [问题描述]C ...
- robotframework - Edit编辑器
1.测试项目&套件 提供的Edit编辑器 2.在 Edit 标签页中主要分:加载外部文件.定义内部变量.定义元数据等三个部分. (1):加载外部文件Add Library:加载测试库,主要是[ ...
- 使用printf和String.format格式化输出
格式化输出 在哪些情况下使用格式化输出: 异常打印到日志中使用格式化输出有利于排查错误原因: printf格式化 示例: public class PrintfTest { public static ...
- css 实现 checkbox 大小调整
一般调整 checkbox 大小我们想到的是 width.height,可是设置后,发现是没有效的. 如微信小程序里面,checkbox 默认就很大,想设置小一点怎么办? transform: sca ...
- JavaScript编程艺术-第10章-10.2-实用的动画
10.2-实用的动画 ***代码亲测可用*** HTML: <!DOCTYPE HTML> <html> <head> <meta charset=" ...