Python simulation queue and Stack (list exercise)

Source: Internet
Author: User

################################# #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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.