[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 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
随机推荐
- 关于hive
这两天在研究了hbase,hadoop,hive,spark 由于spark.py不支持clust(jar才支持,但是太麻烦了>_<) 所以最终决定使用hive 在hive中用create ...
- H264--3--NAL层的处理[6]
------------------------------H.264的NAL层处理 ------------------------------ H264以NALU(NAL unit)为单位来支持编 ...
- Java 过滤器、监听器、拦截器的区别
原文:http://www.360doc.com/content/10/0601/09/495229_30616324.shtml 1.过滤器 Servlet中的过滤器Filter是实现了ja ...
- 4.9 Parser Generators
4.9 Parser Generators This section shows how a parser generator can be used to facilitate the constr ...
- bzoj3262 陌上花开——CDQ分治
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3262 第一道CDQ分治题! 看博客:https://www.cnblogs.com/Narh ...
- 在WIN7里IE8的开发人员工具打不开的解决办法
IE8新增了开发人员工具,非常不错,比早期的DevToolbar好用多了.不过在我的Win7下使用的时候偶尔会出现一个莫名其妙的问题,就是整个开发人员工具窗口消失了,打不开了.当你使用win+Tab切 ...
- 14_传智播客iOS视频教程_OC的数据类型
对比一下OC和C差别,首先第一个是数据类型. C语言的数据类型分哪几类?C语言有哪些数据类型? 基本数据类型当然还包括int的一些修饰符.像short.long.long long.unsigned. ...
- IFRAME动态加载触发onload事件(转)
原文地址:http://blog.ops.cc/webtech/javascript/f5nhm.html <body> <script>var iframe = docume ...
- PCB 帆软FineReport安装,布署,配置
公司使用帆软FineReport做为报表平台工具也有一年多时间,而FineReport报表平台与Tomcat Web应用服务是站队在java阵营里,因为相信拥抱微软;.net未来发展会越来越好,所以对 ...
- MySQL性能优化神器Explain
本文涉及:MySQL性能优化神器Explain的使用 简介 虽然使用Explain不能够马上调优我们的SQL,它也不能给予我们一些调整建议,但是它能够让我们了解MySQL 优化器是如何执行SQL 语句 ...