python实现堆栈 后进先出 LIFO
2017-07-18 12:42
162 查看
比较简单的数据结构,直接贴代码
stack=[]
def pushit ():
stack.append(raw_input('enter new string:').strip())
def popit ():
if len(stack)==0:
print 'Cannot pop from an empty stack'
else:
print 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()[0].lower()
except (EOFError,KeyboardInterrupt,IndexError):
choice='q'
print '\nYou picked: [%s]' % choice
if choice not in 'uovq':
print 'Invalid option, try again'
else:
break
if choice == 'q':
break
CMDs[choice]()
if __name__ == '__main__':
showmenu()
参考自python核心编程 p145
stack=[]
def pushit ():
stack.append(raw_input('enter new string:').strip())
def popit ():
if len(stack)==0:
print 'Cannot pop from an empty stack'
else:
print 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()[0].lower()
except (EOFError,KeyboardInterrupt,IndexError):
choice='q'
print '\nYou picked: [%s]' % choice
if choice not in 'uovq':
print 'Invalid option, try again'
else:
break
if choice == 'q':
break
CMDs[choice]()
if __name__ == '__main__':
showmenu()
参考自python核心编程 p145
相关文章推荐
- python之利用堆栈的后进先出的原理实现倒序
- Python 使用list实现堆栈 (基于class, 包含迭代器)
- 堆栈的实现(LIFO、链表)
- Stack和Queue:后进先出和先进先出和Java 实现后进先出(LIFO)及Stac和Java集合类: Set、List、Map、Queue使用场景梳理 - .Little Hann
- python堆栈实现
- Python基于list的append和pop方法实现堆栈与队列功能示例
- Python 使用单链表实现堆栈 (基于class, 包含迭代器)
- Python实现堆栈和队列<转>
- Python 使用list实现简单的堆栈
- python实现堆栈与队列的方法
- python实现堆栈和队列
- python实现堆栈和队列
- 用python实现堆栈和队列
- java:stack栈: Stack 类表示后进先出(LIFO)的对象堆栈
- 用Python实现的数据结构与算法:堆栈
- python实现堆栈与队列的方法
- 使用java实现后进先出的堆栈功能【笔试题】
- python算法和数据结构笔记--python下堆栈的实现及其应用
- 下压堆栈的链表实现(LIFO)
- Java 实现后进先出(LIFO)