[Data Structure] Stack Implementation in Python
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)
- class Empty(Exception):
- pass
- class ArrayStack:
- """LIFO Stack implementation using Python"""
- def __init__(self):
- self._data=[]
- def __len__(self):
- return len(self._data)
- def is_empty(self):
- return len(self._data)==0
- def push(self,e):
- self._data.append(e)
- def pop(self):
- if self.is_empty():
- raise Empty('Stack is empty')
- return self._data.pop()
- def top(self):
- if self.is_empty():
- raise Empty('Stack is empty')
- return self._data[-1]
[Data Structure] Stack Implementation in Python的更多相关文章
- uva 11995 I Can Guess the Data Structure stack,queue,priority_queue
题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- Data Structure Stack: Reverse a stack using recursion
http://www.geeksforgeeks.org/reverse-a-stack-using-recursion/ #include <iostream> #include < ...
- Data Structure Stack: Infix to Postfix
http://geeksquiz.com/stack-set-2-infix-to-postfix/ #include <iostream> #include <vector> ...
- Python: tree data structure
# 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...
- 笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 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 ...
- HDU 5929 Basic Data Structure 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
随机推荐
- spring context 继承
<web-app> <display-name>Archetype Created Web Application</display-name> <conte ...
- Java技巧之双括弧初始化
由于Java语言的集合框架中(collections, 如list, map, set等)没有提供任何简便的语法结构,这使得在建立常量集合时的工作非常繁索.每次建立时我们都要做: 定义一个临时的集合类 ...
- 将16进制unsigned char数组转换成整数
/** * 将unsigned char数组转换成long long数值 * {0x00 0x00 0x20 0x00}转换之后得到8192 * * @param str 数组 * @param le ...
- h5的坑
转自 http://www.mahaixiang.cn 解决各种坑 http://www.mahaixiang.cn/ydseo/1529.html
- std::string 的方法c_str() 和 data() 有什么区别
1.从C++标准上的解释来看,只有一点区别: c_str() 返回一个指向正规C字符串的指针常量,该指针保证指向一个 size() + 1 长度的空间,而且最后一个字符肯定是 \0 : 而 data( ...
- Win10系列:UWP界面布局进阶5
提示框 在Windows应用商店应用程序中可以使用提示框来向用户显示提示信息,例如可以通过对话框来询问用户当前需要执行的操作,还可以通过弹出窗口来显示需要注意的信息.本节将向读者介绍如何在Window ...
- LTP(LinuxTest Project)测试工具
LTP(LinuxTest Project)是SGI.IBM.OSDL和Bull合作的项目,目的是为开源社区提供一个测试套件,用来验证Linux系统可靠性.健壮性和稳定性.LTP测试套件是测试Linu ...
- learning at command AT+CSUB
[Purpose] Learning how to get mobile module info [Eevironment] Shell terminal, base on gcom command ...
- webpack分离打包css和less
github仓库:https://github.com/llcMite/webpack.git 为什么要分离打包? 答:刚开始学webpack的时候就很郁闷,明明没几个文件,打包出来体积特 ...
- unity中UI的屏幕自适应代码
public void ScreenUISelfAdptation(Transform scaleUI) { float widthrate = UnityEngine.Screen.width / ...