[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. 容器LinkedList原理(学习)

    一.概述 数据结构和ArrayList有本质不同,LinkedList 是基于链表实现,它的插入和删除操作比 ArrayList 更加高效,基于链表的,所以随机访问的效率要比 ArrayList 差. ...

  2. vim升级到8.0

    1.卸载 sudo apt-get remove --purge vim 2.添加8.0的vim源并安装 sudo add-apt-repository ppa:jonathonf/vim sudo ...

  3. 698C

    Description n个视频,长度为k的缓存,每次询问,每个视频以pi的概率被选,如果不在缓存区则加入,如果缓存区满了,则最先进缓存的出来,问10^100次操作以后每个视频在缓存的概率 Input ...

  4. Spark GraphX 属性图操作

    package Spark_GraphX import org.apache.spark.graphx._ import org.apache.spark.rdd.RDD import org.apa ...

  5. 2-4 原生小程序 - 自带组件及API

    获取用户的信息有两种,一种是不需要登录的,我们只需要获取用户的头像,还有微信的名.还有一种是登录的,就是登录后台校验的,就是获取用户的openid. webview,可以内嵌一个网页,类似于原生的开发 ...

  6. P3990 [SHOI2013]超级跳马

    传送门 首先不难设\(f[i][j]\)表示跳到\((i,j)\)的方案数,那么不难得到如下转移 \[f[i][j]=\sum\limits_{k=1}^{\frac n2}f[i-2k+1][j-1 ...

  7. 为什么要用Go语言做后端

    FMZ数字货币量化平台 www.fmz.com, 后端使用Go语言,这里是创始人Zero谈论使用Go语言所带了的便利.原帖地址:https://www.zhihu.com/question/27172 ...

  8. 数据结构之链式队列(C实现)

    1.1  linkqueue.h #ifndef LINKQUEUE_H #define LINKQUEUE_H #include <stdio.h> #include <mallo ...

  9. CF804B Minimum number of steps

    思路: 找规律.参考了http://blog.csdn.net/harlow_cheng/article/details/71190999. 实现: #include <iostream> ...

  10. Android Programming 3D Graphics with OpenGL ES (Including Nehe's Port)

    https://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html