Python uses the list to implement a non-marginal priority queue (class-based, including iterators)

Source: Internet
Author: User
Tags python list

#!/usr/bin/python #-*-Coding:utf-8-*-"Created on 2015-2-4@author:beyondzhou@name:test_listpriorityqueue.py" Def test_listpriorityqueue (): # import Pylistqueue from myqueue import listpriorityqueue print ' #Init a Q    Ueue named Smith using Enqueue ' Smith = Listpriorityqueue () smith.enqueue (' Purple ', 5) smith.enqueue (' Black ', 1)            Smith.enqueue (' Orange ', 3) smith.enqueue (' White ', 0) smith.enqueue (' green ', 1) smith.enqueue (' Yellow ', 5) print ' \n#output Smith queue ' for element in Smith:print element print ' \n#dequeue one ite             M ' Smith.dequeue () print ' \n#output Smith after Dequeue ' for element in Smith:print element print ' \n#dequeue another item ' Smith.dequeue () print ' \n#output Smith after dequeue another item ' for Ele ment in Smith:print element print ' \n#get the length of the queue ' print ' The lenght of the 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_listpriorityqueue ()

# Implementation of the unbounded priority Queue ADT using a Python list# with new items appended to the Endclass Listprio Rityqueue: # Create An empty unbounded priority queue Def __init__ (self): Self._qlist = List () # Returns T     Rue if the queue is empty def isEmpty (self): return len (self) = = 0 # Returns The number of items in the queue def __len__ (self): return Len (self._qlist) # Adds The given item to the queue Def enqueue (self, item, PRI ority): # Create A new instance of the storage class and append it to the list entry = _listpriorityqentry (i         TEM, Priority) Self._qlist.append (entry) # removes and returns the first item in the Queue Def dequeue (self):                Assert not Self.isempty (), "cannot dequeue from an empty queue." # Find the entry with the highest priority highest = Self._qlist[0].priority index = 0 for i in range (Self.__len__ ()): # See if the ith entry ContaiNS a higher priority (smaller integer).                        If self._qlist[i].priority < Highest:highest = Self._qlist[i].priority index = i                    # Remove the entry with the highest priority and return the Item entry = Self._qlist.pop (index) Return Entry.item # Returns the array queue ' s iterator for traversing the elements def __iter__ (self ): Return _listpriorityqueueiterator (self._qlist) # Private storage class for associating queue items with their PRI Orityclass _listpriorityqentry (object): Def __init__ (self, item, priority): Self.item = Item Self.priorit y = priority# implementation of Iterclass _listpriorityqueueiterator:def __init__ (self, thelist): Self._setItem s = thelist Self._curitem = 0 def __iter__ (self): return self def next (self): if Self._curitem < Len (self._setitems): item = Self._setitems[self._curitem] Self._curiTEM + = 1 return item.item, item.priority else:raise stopiteration 

#Init a queue named Smith using Enqueue#output Smith queue (' Purple ', 5) (' Black ', 1) (' Orange ', 3) (' White ', 0) (' green ', 1) (' Yellow ', 5) #dequeue one item#output Smith after dequeue (' Purple ', 5) (' Black ', 1) (' Orange ', 3) (' Green ', 1) (' Yellow ', 5) #de Queue another item#output Smith after dequeue another item (' Purple ', 5) (' Orange ', 3) (' Green ', 1) (' Yellow ', 5) #get the Leng Th of queuethe lenght of the queue is  4#check wheter the queue is Emptystack are not empty! #dequeue all Items#check wheter The queue is empty after dequeue all itemsstack are empty!

Python uses the list to implement a non-marginal priority queue (class-based, including iterators)

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.