-
Notifications
You must be signed in to change notification settings - Fork 10
/
PriorityQueue.py
36 lines (31 loc) · 1.03 KB
/
PriorityQueue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class PriorityQueue(object):
def __init__(self, greatest=False):
self.queue = []
self.greatest = greatest
def __str__(self):
return ' '.join([str(i[0]) for i in self.queue])
# for checking if the queue is empty
def isEmpty(self):
return len(self.queue) == 0
def isNotEmpty(self):
return not self.isEmpty()
# for inserting an element in the queue
def add(self, data, priority):
self.queue.append((data, priority))
# for popping an element based on Priority
def pop(self):
try:
index = 0
for i in range(len(self.queue)):
if self.greatest:
if self.queue[i][1] > self.queue[index][1]:
index = i
else:
if self.queue[i][1] < self.queue[index][1]:
index = i
item = self.queue[index]
del self.queue[index]
return item[0]
except IndexError:
print()
exit()