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. Ogre中OctreeSceneManager

    转自:http://blog.csdn.net/yanonsoftware/article/details/1067265 既然前面分析Mesh(Entity,SceneNode)的渲染时已经看到了O ...

  2. sql server和oracle的差异

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

  3. comet

    comet 1.简介: 基于 HTTP长连接的“服务器推”技术,是一种新的 Web 应用架构,基于这种架构开发的应用中,服务器端会主动以异步的方式向客户端程序推送数据,而不需要客户端显式的发出请求.C ...

  4. 北大poj-1001

    Description Problems involving the computation of exact values of very large magnitude and precision ...

  5. php大力力 [024节]PHP中的字符串连接操作(2015-08-27)

    2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作  阅读:次   时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...

  6. ERP仓库管理系统查询(十)

    需求:    1.根据仓库编号,获取仓库信息绑定至页面相关控件. 2.根据仓库编号,获取管理员信息绑定到页面相关控件 修改的界面: <%@ Page Language="C#" ...

  7. 通过一段代码说明C#中rel与out的使用区别

    using System; public partial class testref : System.Web.UI.Page { static void outTest(out int x, out ...

  8. 六、CCLayer

    一个游戏中可以有很多个场景,每个场景里面又可能包含有多个图层,这里的图层一般就是CCLayer对象.CCLayer本身几乎没什么功能,对比CCNode,CCLayer可用于接收触摸和加速计输入.其实, ...

  9. latex figure \label 放在\caption 后

    http://www.cnblogs.com/loca/p/4264686.html latex figure \label 放在\caption 后,否则将显示\section 或者\subsect ...

  10. Node.js简单介绍并实现一个简单的Web MVC框架

    编号:1018时间:2016年6月13日16:06:41功能:Node.js简单介绍并实现一个简单的Web MVC框架URL :https://cnodejs.org/topic/4f16442cca ...