-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.py
29 lines (25 loc) · 871 Bytes
/
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
class PriorityQueue:
#Implementing priority queue using stacks.
def __init__(self):
self.element=[]
self.length=0
def enqueue(self,priority,value):
newElement={'priority':priority,'value':value}
if self.length==0:
self.element.append(newElement)
else:
for i in range(self.length):
if self.element[i]['priority'] >= priority:
self.element.insert(i,newElement)
self.length+=1
return self.element
self.element.append(newElement)
self.length+=1
return self.element
def dequeue(self):
if self.length==0:
return 'Queue is empty'
else:
self.element.pop()
self.length-=1
return self.element