Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)

来源:互联网 发布:盗取数据 编辑:程序博客网 时间:2024/05/21 23:55
#!/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.enqueue('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#dequeue one item'    smith.dequeue()        print '\n#output smith after dequeue'    for element in smith:        for i in element:            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 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_bpriorityqueue()

from myarray import Array# Implementation of the bounded Priority Queue ADT using an array of# queues in which the queues are 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 = Array(numLevels)        for i in range(numLevels):            self._qLevels[i] = linkListQueue()    # Returns True if the queue is empty    def isEmpty(self):        return len(self) == 0    # Returns the number of items in the queue    def __len__(self):        return self._qSize    # Adds the given item to the queue    def enqueue(self, item, priority):        assert priority >= 0 and priority < len(self._qLevels), \                    "Invalid priority level."        self._qLevels[priority].enqueue(item)        self._qSize += 1    # Removes and returns the next item in the queue    def dequeue(self):        # Make sure the queue is not empty        assert 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, so dequeue from the ith queue        self._qSize -= 1        return self._qLevels[i].dequeue()        # Returns the array queue's iterator for 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(self):        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 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!

0 0
原创粉丝点击