We can realize a Stack as an adaptation of a Python List.

S.push(e)=L.append(e)

S.pop()=L.pop()

S.top()=L[-1]

S.len()=len(L)

S.is_empty=(len(L)==0)

  1. class Empty(Exception):
  2. pass
  3.  
  4. class ArrayStack:
  5. """LIFO Stack implementation using Python"""
  6.  
  7. def __init__(self):
  8. self._data=[]
  9.  
  10. def __len__(self):
  11. return len(self._data)
  12.  
  13. def is_empty(self):
  14. return len(self._data)==0
  15.  
  16. def push(self,e):
  17. self._data.append(e)
  18.  
  19. def pop(self):
  20. if self.is_empty():
  21. raise Empty('Stack is empty')
  22. return self._data.pop()
  23.  
  24. def top(self):
  25. if self.is_empty():
  26. raise Empty('Stack is empty')
  27. return self._data[-1]

  

[Data Structure] Stack Implementation in Python的更多相关文章

  1. uva 11995 I Can Guess the Data Structure stack,queue,priority_queue

    题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...

  2. Data Structure Stack: Reverse a stack using recursion

    http://www.geeksforgeeks.org/reverse-a-stack-using-recursion/ #include <iostream> #include < ...

  3. Data Structure Stack: Infix to Postfix

    http://geeksquiz.com/stack-set-2-infix-to-postfix/ #include <iostream> #include <vector> ...

  4. Python: tree data structure

    # 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...

  5. 笔记-python-tutorial-5.data structure

    笔记-python-tutorial-5.data structure 1.      data structure 1.1.    list operation list.append(x) #尾部 ...

  6. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  7. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  8. UVa 11995:I Can Guess the Data Structure!(数据结构练习)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  9. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. spring context 继承

    <web-app> <display-name>Archetype Created Web Application</display-name> <conte ...

  2. Java技巧之双括弧初始化

    由于Java语言的集合框架中(collections, 如list, map, set等)没有提供任何简便的语法结构,这使得在建立常量集合时的工作非常繁索.每次建立时我们都要做: 定义一个临时的集合类 ...

  3. 将16进制unsigned char数组转换成整数

    /** * 将unsigned char数组转换成long long数值 * {0x00 0x00 0x20 0x00}转换之后得到8192 * * @param str 数组 * @param le ...

  4. h5的坑

    转自 http://www.mahaixiang.cn 解决各种坑 http://www.mahaixiang.cn/ydseo/1529.html

  5. std::string 的方法c_str() 和 data() 有什么区别

    1.从C++标准上的解释来看,只有一点区别: c_str() 返回一个指向正规C字符串的指针常量,该指针保证指向一个 size() + 1 长度的空间,而且最后一个字符肯定是 \0 : 而 data( ...

  6. Win10系列:UWP界面布局进阶5

    提示框 在Windows应用商店应用程序中可以使用提示框来向用户显示提示信息,例如可以通过对话框来询问用户当前需要执行的操作,还可以通过弹出窗口来显示需要注意的信息.本节将向读者介绍如何在Window ...

  7. LTP(LinuxTest Project)测试工具

    LTP(LinuxTest Project)是SGI.IBM.OSDL和Bull合作的项目,目的是为开源社区提供一个测试套件,用来验证Linux系统可靠性.健壮性和稳定性.LTP测试套件是测试Linu ...

  8. learning at command AT+CSUB

    [Purpose] Learning how to get mobile module info [Eevironment] Shell terminal, base on gcom command ...

  9. webpack分离打包css和less

    github仓库:https://github.com/llcMite/webpack.git 为什么要分离打包?       答:刚开始学webpack的时候就很郁闷,明明没几个文件,打包出来体积特 ...

  10. unity中UI的屏幕自适应代码

    public void ScreenUISelfAdptation(Transform scaleUI) { float widthrate = UnityEngine.Screen.width / ...