Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@irachex
Created October 20, 2012 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irachex/3922688 to your computer and use it in GitHub Desktop.
Save irachex/3922688 to your computer and use it in GitHub Desktop.
PriorityQueue implements in Python
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