首页 > 编程语言 > 详细

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

时间:2015-02-06 14:59:49      阅读:405      评论:0      收藏:0      [点我收藏+]
#!/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 Array
class 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 iter
class _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 queue
white
black
green
orange
purple
yellow

#dequeue one item

#output smith after dequeue
black
green
orange
purple
yellow

#dequeue another item

#output smith after dequeue another item
green
orange
purple
yellow

#get the length of queue
the lenght of queue is  4

#check wheter the queue is empty
stack is not empty!

#dequeue all items

#check wheter the queue is empty after dequeue all items
stack is empty!

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

原文:http://blog.csdn.net/guaguastd/article/details/43563513

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!