1.实现栈:

stack=[]

def pushit():
stack.append(raw_input('Enter New String:').strip())
def popit():
if len(stack)==0:
print 'can not pop anything from a empty stack'
else:
print 'Remove[',repr(stack.pop()),']'
def viewstack():
print stack CMDs={'u':pushit,'o':popit,'v':viewstack} def showmenu():
pr="""
p(U)sh
p(O)p
(V)iew
(Q)uit Enter choice:"""
while True:
while True:
try:
choice=raw_input(pr).strip().lower()
except (EOFError,KeyboardInterrupt,IndexError):
choice='q'
print '\nYou picked:[%s]' %choice
if choice not in 'uovq':
print'Invaild option,try again'
else:
break
if choice=='q':
break
CMDs[choice]() if __name__=='__main__':
showmenu()

2.实现队列

queue=[]

def enQ():
queue.append(raw_input('Please enter a new element:').strip()) def deQ():
if len(queue)==0:
print "Error: cannot pop anything from a empty queue"
else:
print 'Remove [',queue.pop(0),']' def viewshow():
print queue
CMDs={'e':enQ,'d':deQ,'v':viewshow}
def showmenu():
pr='''
(E)nQ
(D)eQ
(V)iewshow
(Q)uit Enter your choice:
'''
while True:
while True:
try:
choice=raw_input(pr).strip()[0].lower()
except (EOFError,KeyboardInterrupt,IndexError):
choice='q'
print 'You picked [%s]' % choice
if choice not in 'edvq':
print 'Invail option, Try again'
else:
break
if choice=='q':
break
CMDs[choice]()
if __name__=='__main__':
showmenu()

Python列表操作——模拟实现栈和队列的更多相关文章

  1. Python列表操作大全(非常全)

    Python列表操作大全(非常全!!!) 对于python列表的理解可以和C语言里面的数组进行比较性的记忆与对照,它们比较相似,对于python里面列表的定义可以直接用方括号里加所包含对象的方法,并且 ...

  2. python第七篇:Python 列表操作详解

    Python列表操作详解 list函数 list()   #生成一个空的列表 list(iterable)  #用可迭代对象初始化一个列表 列表的 and 运算和 or 运算 列表and运算 > ...

  3. python列表操作大全

    Python列表操作大全 对于python列表的理解可以和C语言里面的数组进行比较性的记忆与对照,它们比较相似,对于python里面列表的定义可以直接用方括号里加所包含对象的方法,并且python的列 ...

  4. Python列表操作集合

    对于python列表里元素的操作主要分为以下几个方面: 1.向列表里面加元素: 向python列表里面添加元素主要有三种方法: (1)append() append()对于列表的操作主要实现的是在特定 ...

  5. Python列表操作与深浅拷贝(5)——数字处理函数、类型判断、列表链表队列栈

    python内建数据结构 分类 数值型: int float complex bool 序列对象: 字符串str 列表list 元组tuple 键值对: 集合set 字典dict 数值型 (list ...

  6. python列表操作总结

    list是python中非常重要的类型/数据结构,总结如下: 符号说明 l:列表 l2:新列表 e:元素 index:位置 方法: 原地修改: l.append(e) l.insert(index, ...

  7. python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)

    今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...

  8. Python :用两个栈实现队列

    转自:http://blog.csdn.net/Lynette_bb/article/details/75092745 牛客网上的剑指 offer的在线编程: 题目描述 用两个栈来实现一个队列,完成队 ...

  9. Python列表操作常用API

    1.列表的概念 (1)列表的定义 列表是Python中一种基本的数据结构.列表存储的数据,我们称为元素.在列表中的每个元素都会有一个下标来与之对应,第一个索引是0,第二个索引是1,依此类推的整数. 列 ...

随机推荐

  1. Stm32_调试出现 Error:Flash Download Failed-"Cortex-M3"

    rror:Flash Download Failed-"Cortex-M3"出现一般有两种情况: 1.SWD模式下,Debug菜单中,Reset菜单选项(Autodetect/HW ...

  2. sql server和oracle的差异

    .部分SQL语句差异 (1)SQL:select top 10 * from table     ORA: select * from table where rownum<11(2)SQL:S ...

  3. webservice实验一

    实验目的:安装jdk1.6_21以后的版本,利用JAX-WS API自己发布webservice并调用,以及用wsimport生成webservice客户端代码调用一个免费的web服务(如webxml ...

  4. ios 从网络上获取图片并在UIImageView中显示

    ios 从网络上获取图片   -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...

  5. false等于0???

    看到一个函数strpos($string,$str),用于在字符串$string中查找$str,如果在$string中查找到$str,则返回第一次出现的位置,起始位置为0:如果$string中不包含$ ...

  6. VMware与Cisco etherchannel

    命令样例: ①把要绑定team的端口添加到channel-group interface Te2/1/1 switchport switchport access vlan 123 switchpor ...

  7. 定时同步时间与crontab

    date 月日时分年.秒date -s可以直接设置系统时间 比如将系统时间设定成1996年6月10日的命令如下.#date -s 06/10/96将系统时间设定成下午1点12分0秒的命令如下.#dat ...

  8. HDU 5823 (状压dp)

    Problem color II 题目大意 定义一个无向图的价值为给每个节点染色使得每条边连接的两个节点颜色不同的最少颜色数. 对于给定的一张由n个点组成的无向图,求该图的2^n-1张非空子图的价值. ...

  9. if语句解一元二次方程~

    #include<stdio.h>#include<math.h> void main(){  double a,b,c,x1,x2;  printf("请输入a&q ...

  10. 【转】IoC/DIP其实是一种管理思想

    原文转自:http://blogread.cn/it/article/6487?f=wb 关于IoC的的概念提出来已经很多年了,其被用于一种面象对像的设计.我在这里再简单的回顾一下这个概念.我先谈技术 ...