Python Data Structure queue implementation, python queue

Source: Internet
Author: User

Python Data Structure queue implementation, python queue

Python queue

A Queue is a type of first-in-first-out (FIFO) data. A new element is added to the end of a Queue by joining the Queue. A new element is deleted from the Queue header.

Use the list as the Queue:

Queue = [] # initialize a list data type object as a queue def enQ (): # define an inbound stack method queue. append (raw_input ('enter New String :'). strip () # prompt to enter a String object for the queue and call Str. strip () ensures that the input String value does not contain any extra space def deQ (): # define a queuing method if len (queue) = 0: print "Cannot pop from an empty queue! "Else: print 'remove [', 'queue. pop (0) ','] '# Use reverse single quotes ('') instead of repr (), and extend the String value with quotation marks, not only the String value # queue. pop (0) always pops up def viewQ (): # defines a method to display the content in the queue. print queueCMDs = {'U': enQ, 'o': deQ, 'V': viewQ} # define a Dict type object and map characters to the corresponding function. you can enter a character to execute the corresponding operation def showmenu (): # define an operation menu prompt method pr = "(E) nqueue (D) equeue (V) iew (Q) uit Enter choice: "" while True: try: choice = raw_input (pr ). strip () [0]. lower () # Str. strip () removes extra spaces before and after the String object # Str. lower () converts multiple inputs into lower-case ones to facilitate unified judgment in the future # input ^ D (EOF, generates an EOFError exception) # input ^ C (interrupt and exit, generates a keyboardInterrupt exception) couldn t (EOFError, KeyboardInterrupt, IndexError): choice = 'q' print '\ nYou picked: [% s]' % choice if choice not in 'uovq': print 'invalid option, try again 'else: break if choice = 'q': break CMDs [choice] () # obtain the functionName corresponding to the character in Dict, implement function call if _ name _ = '_ main _': showmenu ()

The queue and stack are implemented in a similar way. The difference is that the first element always pops up in the queue and the last element always pops up in the stack.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.