Created
October 20, 2012 08:34
-
-
Save irachex/3922688 to your computer and use it in GitHub Desktop.
PriorityQueue implements in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
class PriorityQueueNode(object): | |
def __init__(self, id, value): | |
self.id = id | |
self.value = value | |
class PriorityQueue(object): | |
def __init__(self, k): | |
self.q = deque() | |
def head(self, i): | |
while self.q and self.q[0].id < i: | |
self.q.popleft() | |
if not self.q: return None | |
return self.q[0] | |
def append(self, id, value): | |
while self.q and self.q[-1].value <= value: | |
self.q.pop() | |
self.q.append(PriorityQueueNode(id, value)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment