Python implements the queue method, and python implements the queue
This example describes how to implement a queue in Python. Share it with you for your reference. The specific implementation method is as follows:
#! /Usr/bin/env python queue = [] def enQ (): queue. append (raw_input ('enter new string :'). strip () # Call the pop () function of the list. pop (0) is the first element in the list def deQ (): if len (queue) = 0: print 'cannot pop from an 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: try: choice = raw_input (pr ). strip () [0]. lower () handle T (EOFError, KeyboardInterrupt, 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 Python programming.