python模拟队列和堆栈(列表练习)-创新互联
##################################queue######################################### #!/usr/bin/env python 'use list as a queue' #define a void list as a void queue queue = [] #define in queue function def enQ(): queue.append(raw_input('Enter New String: ').strip()) #define out queue function def deQ(): #judge queue whether viod if len(queue) == 0: print('Can not pop from an empty queue!') else: print('Removed' ,queue.pop(0)) #define show queue function def viewQ(): print(queue) #define a dictionary to chose opration function cmds = {'e':enQ,'d':deQ,'v':viewQ} #define a funtion to show menu def showMenu(): pr = ''' (E)nqueue (D)equeue (V)iew (Q)uit Enter choice:''' #double while circle make program always run while True: while True: try: #use to print menu information and get valid choice number(no space, just one bit , lower) choice = raw_input(pr).strip()[0].lower() except(EOFError,KeyboardInterrupt,IndexError): #if get a invalid value,return 'q' choice = 'q' print('You picked: %s '% choice) if choice not in 'devq': print('Invalid option, try again!') else: break if choice == 'q': break #call functions by dictionary cmds[choice]() #main function if __name__ == '__main__': showMenu() ##################################stack###################################### #!/usr/bin/env python 'this program use list as a stack' stack = [] def pushit(): 'input stack ' stack.append(raw_input('Enter New Strings: ').strip()) def popit(): 'output stack' if len(stack) == 0: print('Can not pop from an empty stack!') else: print('removed [',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() print(choice) except(EOFError.KeyboardInterrupt,IndexError): choice = 'q' print('you 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()目前成都创新互联公司已为成百上千家的企业提供了网站建设、域名、网页空间、绵阳服务器托管、企业网站设计、南岸网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前名称:python模拟队列和堆栈(列表练习)-创新互联
文章来源:http://scyanting.com/article/csscjc.html