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
- Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)
- Python 使用list实现无边际优先队列 (基于class, 包含迭代器)
- Python 使用循环数组实现队列 (基于class, 包含迭代器)
- Python 使用单链表实现队列 (基于class, 包含迭代器)
- Python 使用list实现队列 (基于class, 包含迭代器)
- Python 使用单链表实现堆栈 (基于class, 包含迭代器)
- Python 使用list实现堆栈 (基于class, 包含迭代器)
- 优先队列的数组实现
- 基于数组的最大优先队列
- 队列的实现—由数组实现
- 基于堆的优先队列的实现
- 基于堆的优先队列实现
- 优先队列的数组、二叉堆实现
- 优先队列的数组实现(有序)
- 基于condition 实现的线程安全的优先队列(python实现)
- Python实现优先队列
- Python实现优先队列
- 使用优先队列构建赫夫曼树
- jsp中,带有"百分号%"的值如何用request.getParameter取得
- dukuwiki迁移
- 中国城市新分级名单(转)
- 使用jquery获取url以及jquery获取u…
- Unity新GUI教程 – 第一部分
- Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)
- 用Excel展示SQL Server中的数…
- MySQL存储过程详解 mysql …
- 【MySQL】触发器入门(转)
- Restaurant Menus
- 抽水马桶工作原理演示
- 绿色版浏览器DIY,大家别乱用网上的非官方绿色版,自己动手吧,很简单!(2015-11-26更新)
- mysql语句-load data infile
- HTML处理收集(转)