Python 使用list实现无边际优先队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*-'''Created on 2015-2-4@author: beyondzhou@name: test_listpriorityqueue.py'''def test_listpriorityqueue():# import pyListQueuefrom myqueue import ListPriorityQueueprint '#Init a queue 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 elementprint '\n#dequeue one item'smith.dequeue()print '\n#output smith after dequeue'for element in smith:print elementprint '\n#dequeue another item'smith.dequeue()print '\n#output smith after dequeue another item'for element in smith:print elementprint '\n#get the length of 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'while 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 ListPriorityQueue:# Create an empty unbounded priority queuedef __init__(self):self._qList = list()# Returns True if the queue is emptydef isEmpty(self):return len(self) == 0# Returns the number of items in the queuedef __len__(self):return len(self._qList)# Adds the given item to the queuedef enqueue(self, item, priority):# Create a new instance of the storage class and append it to the listentry = _ListPriorityQEntry(item, priority)self._qList.append(entry)# Removes and returns the first item in the queuedef dequeue(self):assert not self.isEmpty(), "Cannot dequeue from an empty queue."# Find the entry with the highest priorityhighest = self._qList[0].priorityindex = 0for 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].priorityindex = i# Remove the entry with the highest priority and return the itementry = self._qList.pop(index)return entry.item# Returns the array queue's iterator for traversing the elementsdef __iter__(self):return _ListPriorityQueueIterator(self._qList)# Private storage class for associating queue items with their priorityclass _ListPriorityQEntry(object):def __init__(self, item, priority):self.item = itemself.priority = priority# Implementation of iterclass _ListPriorityQueueIterator:def __init__(self, theList):self._setItems = theListself._curItem = 0def __iter__(self):return selfdef next(self):if self._curItem < len(self._setItems):item = self._setItems[self._curItem]self._curItem += 1return item.item, item.priorityelse: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)#dequeue another item#output smith after dequeue another item('purple', 5)('orange', 3)('green', 1)('yellow', 5)#get the length of queuethe lenght of queue is 4#check wheter the queue is emptystack is not empty!#dequeue all items#check wheter the queue is empty after dequeue all itemsstack is empty!

,人生没有停靠站,自我本身永远是一个出发点。

Python 使用list实现无边际优先队列 (基于class, 包含迭代器)

相关文章:

你感兴趣的文章:

标签云: