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: