#!/usr/bin/python #-*-Coding:utf-8-*-"Created on 2015-2-6@author:beyondzhou@name:test_bpriorityqueue.py" def Test_bpriorityqueue (): # import Pylistqueue from myqueue import bpriorityqueue print ' #Init a queue named Smith using Enqueue ' Smith = Bpriorityqueue (6) smith.enqueue (' Purple ', 5) smith.enqueue (' Black ', 1) smith.enq Ueue (' Orange ', 3) smith.enqueue (' White ', 0) smith.enqueue (' green ', 1) smith.enqueue (' Yellow ', 5) print ' \n#output Smith queue ' for element in smith:for i in Element:print i print ' \n#dequ Eue one item ' Smith.dequeue () print ' \n#output Smith after Dequeue ' for element in Smith:for I in ele Ment:print i print ' \n#dequeue another item ' Smith.dequeue () print ' \n#output Smith after Dequeue another item ' for element in smith:for i in Element:print i print ' \n#get the L Ength of the queue ' print ' thE Lenght of queue is ', Len (Smith) print ' \n#check wheter the queue is empty ' if Smith.isempty (): print ' Stack is empty! ' Else:print ' stack is not empty! ' print ' \n#dequeue all items ' and not Smith.isempty (): Smith.dequeue () print ' \n#check wheter the queue is empty after dequeue all items ' if Smith.isempty (): print ' stack is empty! ' Else:print ' stack is not empty! ' if __name__ = = "__main__": Test_bpriorityqueue ()
From myarray import array# implementation of the bounded priority Queue ADT using a Array of# queues in which the queues is implemented using a linked List#from myarray import arrayclass bpriorityqueue: # Creates an empty bounded priority Queue def __init__ (self, numlevels): self._qsize = 0 Self._numlevels = numlevels self._qlevels = A Rray (numlevels) for I in Range (numlevels): self._qlevels[i] = Linklistqueue () # Returns True if the Q Ueue is empty def isEmpty (self): return len (self) = = 0 # Returns The number of items in the queue def __le N__ (self): return Self._qsize # Adds The given item to the queue Def enqueue (self, item, priority): AS SERT priority >= 0 and Priority < Len (self._qlevels), "Invalid." Self._qlevels[priority].enqueue (item) Self._qsize + = 1 # removes and returns the next item in the queue Def D Equeue (self): # make sureThe queue is not empty asserts not self.isempty (), "cannot dequeue from an empty queue." # Find The first non-empty queue i = 0 p = len (self._qlevels) while I < p:if not self. _qlevels[i].isempty (): Break i + = 1 # We know the queue is not empty and so dequeue from the ITH queue Self._qsize-= 1 return self._qlevels[i].dequeue () # Returns the array queue ' s iterator fo R traversing the Elements def __iter__ (self): return _bpriorityqueueiterator (self._qlevels) # implementation of Iterclass _bpriorityqueueiterator:def __init__ (self, qlevels): Self._qlevels = Qlevels Self._curitem = 0 def __iter__ (self): return self def Next: items = [] If Self._curitem < Len (self._ Qlevels): For I in Self._qlevels[self._curitem]: items.append (i) Self._curitem + = 1 Return items Else: Raise Stopiteration
#Init a queue named Smith using Enqueue#output Smith queuewhiteblackgreenorangepurpleyellow#dequeue one item#output Smith After Dequeueblackgreenorangepurpleyellow#dequeue another item#output Smith after dequeue another Itemgreenorangepurpleyellow#get the length of queuethe lenght of the queue is 4#check wheter the queue was Emptystack is no T empty! #dequeue all Items#check wheter the queue was empty after dequeue all itemsstack are empty!
Python uses an array built from a single-linked list to implement a marginal priority queue (class-based, including iterators)