[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的更多相关文章

  1. python数据结构之栈与队列

    python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...

  2. Python - 数据结构 - 第十五天

    Python 数据结构 本章节我们主要结合前面所学的知识点来介绍Python数据结构. 列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和 ...

  3. Python数据结构汇总

    Python数据结构汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.线性数据结构 1>.列表(List) 在内存空间中是连续地址,查询速度快,修改也快,但不利于频繁新 ...

  4. python数据结构之二叉树的统计与转换实例

    python数据结构之二叉树的统计与转换实例 这篇文章主要介绍了python数据结构之二叉树的统计与转换实例,例如统计二叉树的叶子.分支节点,以及二叉树的左右两树互换等,需要的朋友可以参考下 一.获取 ...

  5. Python数据结构与算法之图的广度优先与深度优先搜索算法示例

    本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法.分享给大家供大家参考,具体如下: 根据维基百科的伪代码实现: 广度优先BFS: 使用队列,集合 标记初始结点已被发现,放入队列 ...

  6. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

  7. python数据结构与算法——链表

    具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...

  8. python数据结构之图的实现

    python数据结构之图的实现,官方有一篇文章介绍,http://www.python.org/doc/essays/graphs.html 下面简要的介绍下: 比如有这么一张图: A -> B ...

  9. Python数据结构与算法--List和Dictionaries

    Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...

随机推荐

  1. luogu1941 飞扬的小鸟

    题目大意 游戏界面是一个长为n ,高为 m 的二维平面,其中有k 个管道(忽略管道的宽度).小鸟始终在游戏界面内移动.小鸟从游戏界面最左边任意整数高度位置出发,到达游戏界面最右边时,游戏完成.小鸟每个 ...

  2. js二维数组定义和初始化的三种方法总结

    js二维数组定义和初始化的三种方法总结 方法一:直接定义并且初始化,这种遇到数量少的情况可以用var _TheArray = [["0-1","0-2"],[& ...

  3. 7-80 HTML5新增的JS选择器

    7-80 HTML5新增的JS选择器 学习要点 HTML5新增的JS选择器 在传统的 JavaScript 开发中,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 ta ...

  4. 详解jQuery uploadify文件上传插件的使用方法

    uploadify这个插件是基于js里面的jquery库写的.结合了ajax和flash,实现了这个多线程上传的功能. 现在最新版为3.2.1. 在线实例 实例中用到的php文件UploaderDem ...

  5. XHTML与HTML区别

    1.一下规则适用于XHTML,但并不适用于HTML <html>.<head>.<body>都是必需的标签 必须设置<html>标签的xmlns属性,且 ...

  6. varnish的架构和日志

    varnish的架构和日志 varnish的架构 知道varnish的内部结构有两个重要的原因: 首先,架构主要负责性能,其次,它影响你如何将Varnish集成到你自己的架构中. 主程序块是Manag ...

  7. tar zxvf 解压文件提示错误

    1.tar -zxvf 提示错误 2. 查看文件之后发现是html格式的.file **(文件名) 3.原来是直接之前sudo wget url ...url连接错误了. 这个url直接在jdk哪里, ...

  8. P3174 [HAOI2009]毛毛虫(树形dp)

    P3174 [HAOI2009]毛毛虫 题目描述 对于一棵树,我们可以将某条链和与该链相连的边抽出来,看上去就象成一个毛毛虫,点数越多,毛毛虫就越大.例如下图左边的树(图 1 )抽出一部分就变成了右边 ...

  9. sublime text2 配置php本地环境时遇到的错误。

    首先,将PHP加到电脑的环境变量中如图(D:\PHPEnv\PHP5是我PHP的安装目录): 第二步:添加编译系统配置 第三步:配置详情: { "cmd": ["php. ...

  10. 反向代理与Real-IP和X-Forwarded-For(转)

    如下图所示,客户端通过Nginx Proxy1 和 Nginx Proxy2 两层反向代理才访问到具体服务Nginx Backend(或如Tomcat服务).那Nginx Backend如何才能拿到真 ...