Example of how to operate a queue in the basic data structure using Python, and how to operate a queue using python

Source: Internet
Author: User

Example of how to operate a queue in the basic data structure using Python, and how to operate a queue using python

This example describes how to implement a queue in the basic data structure in Python. We will share this with you for your reference. The details are as follows:

#! /Usr/bin/env python # coding = utf-8class Queue (object): def _ init _ (self, size): self. size = size self. head =-1 # initialize the team header self. tail =-1 # initialize team-end self. queue = [] def EnQueue (self, x): if self. isFull (): # raise Exception ("overflow! ") Else: self. queue. append (x) self. tail + = 1 # adding elements to the queue is at the end of def DeQueue (self): if self. isEmpty (): # raise Exception ("underflow! ") Else: self. head + = 1 # delete an element from the queue and move it back to return self. queue. pop (0) # Use the built-in function pop () to pop up the team header def IsFull (self): # determine if the queue is full # return (self. tail + 1) % self. size = self. head return self. tail-self.head + 1 = self. size def IsEmpty (self): # judge that the queue is empty !!! Return self. head = self. tailif _ name _ = '_ main _': print "Test Result:" q = Queue (10) for I in range (3): q. enQueue (I) print q. queue print q. deQueue () print q. queue print q. deQueue () print q. isEmpty () print q. deQueue () print q. isEmpty () print q. queue for I in range (9): q. enQueue (I) print q. queue print q. isFull ()

Running result:

Related Article

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.