Created
June 28, 2020 03:19
-
-
Save terror/c679d46ba5ffc67956df8a53516c289e to your computer and use it in GitHub Desktop.
Priority queue implementation using list
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
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