################################# #queue ##########################################!/usr/bin/env python ' use list as a queue ' #define a void list as a void Queuequeue = [] #define  IN QUEUE FUNCTIONDEF ENQ (): Queue.append (raw_input (' enter new string: '). Strip ()) #define out queue Functiondef 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 FUNCTIONDEF VIEWQ (): print (queue) #define a dictionary to chose opration functioncmds = {' E ': EnQ, ' d ':d eq, 'V ': viewq} #define  A FUNTION TO SHOW MENUDEF 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: &Nbsp; break if choice == ' Q ': break #call functions by dictionary cmds[choice] () #main functionif __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: &Nbsp; 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 () &Nbsp; 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&nBsp; cmds[choice] () if __name__ == ' __main__ ': showmenu ()
Python simulation queue and Stack (list exercise)