The example in this article describes how Python implements queues. Share to everyone for your reference. The implementation methods are as follows:
?
| 1, 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#!/usr/bin/env python queue = [] def enQ (): Queue.append (Raw_input (' Enter new string: '). Strip ()) #调用list的列表的pop () function. Pop ( 0 for the first element of the list Def DeQ (): If Len (queue) = = 0:print ' cannot pop from ' a empty queue! ' else:print ' removed ', Queue.pop (0), '] ' def viewq (): print queue CMDs = {' E ': EnQ, ' d ': DeQ, ' V ': VIEWQ} def showmenu (): PR = "" "(e) nqueue (d) equeue (v) iew ( Q uit Enter choice: "" "while true:while True:try:choice = raw_input (pr). Strip () [0].lower () except (Eoferror, Keyboardi Nterrupt, indexerror): choice = ' q ' print ' nyou picked: [%s] '% choice if choice not in ' Devq ': print ' Invalid option, try Again ' else:break if choice = = ' Q ': Break Cmds[choice] () if __name__ = = ' __main__ ': ShowMenu () |
I hope this article will help you with your Python programming.