首页 > 编程语言 > 详细

python 列表模拟堆栰

时间:2015-07-15 15:17:46      阅读:171      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/env python
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:
        r=stack.pop()
        print "Removed [ %s]" %r

def viewstatck():
    print stack

CMDs = {‘u‘:pushit,‘o‘:popit,‘v‘:viewstatck}

def showmenu():
    pr="""
    p(U)sh
    P(O)p
    (V)iew
    (Q)uit
    Enter choice:"""

    while True:
        choice = raw_input(pr).strip()[0].lower()
        print "\nYou picked:[%s]"%choice
        if choice == ‘q‘:
            break
        if choice not in ‘uovq‘:
            print ‘Invalid option,tyr again‘
            continue
        else:
            CMDs[choice]()

if __name__ == ‘__main__‘:
    showmenu()


本文出自 “服务器” 博客,请务必保留此出处http://zhangfang2012.blog.51cto.com/6380212/1674853

python 列表模拟堆栰

原文:http://zhangfang2012.blog.51cto.com/6380212/1674853

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!