Skip to content

Instantly share code, notes, and snippets.

@terror
Created June 28, 2020 03:19
Show Gist options
  • Save terror/c679d46ba5ffc67956df8a53516c289e to your computer and use it in GitHub Desktop.
Save terror/c679d46ba5ffc67956df8a53516c289e to your computer and use it in GitHub Desktop.
Priority queue implementation using list
class PriorityQueue:
def __init__(self):
self.elements = []
# add element to queue
def enqueue(self, element):
self.elements.append(element)
# poll and return element based on priority
# note that this is max pq
def dequeue(self):
try:
Max = 0
for i in range(len(self.elements)):
if self.elements[i] > self.elements[Max]:
Max = i
element = self.elements[Max]
del self.elements[Max]
return element
except IndexError:
exit()
def isEmpty(self):
return self.elements == []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment