[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 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
随机推荐
- Bing必应地图中国API入门讲座之八:显示驾车路线
Bing必应地图中国API入门讲座之八:显示驾车路线 2011-05-24 14:47:36| 分类: Bing&Google|字号 订阅 这篇文章非常值得纪念,因为我是在Googl ...
- 如何抓取基于https协议的webservice数据包
方法一:基于Fiddler2等第三方工具(需要在Java端禁用SSL安全检查) 原文拷贝自http://blog.csdn.net/zmxj/article/details/6327775,向原作者表 ...
- 7 Worksheet 对象
7.1 设置阶段 代码清单7.1:使用Parent属性获得一个对象的父对象的指针 '使用Parent属性获得一个对象的父对象的指针 Sub MeetMySingleParent() 'Declare ...
- Spark 二项逻辑回归__二分类
package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.{B ...
- bzoj 4806: 炮【dp】
同1801 注意到一行只能放012个炮,我们只需要知道列的状态,不用状压行 所以设f[i][j][k]表示前i行有j列有1个炮,有k列有2个炮的方案数 然后分情况讨论转移就行了 #include< ...
- 1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1219 Solved: ...
- ASP.NET SQL 总结
1.SQLSERVER服务器中,给定表 table1 中有两个字段 ID.LastUpdateDate,ID表示更新的事务号, LastUpdateDate表示更新时的服务器时间,请使用一句SQL语句 ...
- EditText(5)如何控制输入键盘的显示方式及参数表
参考: https://developer.android.com/training/keyboard-input/visibility.html 1,进入Activity时,EditText就获得焦 ...
- C# 生成 bmp 格式的图片
using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; usin ...
- 【转】HIVE UDF UDAF UDTF 区别 使用
原博文出自于:http://blog.csdn.net/longzilong216/article/details/23921235(暂时) 感谢! 自己写代码时候的利用到的模板 UDF步骤: 1 ...