zoukankan      html  css  js  c++  java
  • Python列表操作——模拟实现栈和队列

    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 '
    You 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()
  • 相关阅读:
    C# vb实现浮雕特效滤镜效果
    一张图看懂SharpImage
    C#控制操控操作多个UVC摄像头设备
    C#读写修改设置调整UVC摄像头画面-缩放
    继承多态绕点 Java篇
    继承多态绕点 C#篇
    lock关键字理解
    关于C#迭代器
    关于排列组合中组合结果
    C#与Java中相等关系
  • 原文地址:https://www.cnblogs.com/itdyb/p/5382492.html
Copyright © 2011-2022 走看看