-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.py
More file actions
34 lines (29 loc) · 893 Bytes
/
Copy pathpriority_queue.py
File metadata and controls
34 lines (29 loc) · 893 Bytes
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
class PRQNode:
def __init__(self, state, path="", priority=0):
self.state = state
self.path = path
self.priority = priority
self.next = None
class PriorityQueue:
def __init__(self):
self.head = None
def push(self, new):
if self.isEmpty():
self.head = new
elif self.head.priority >= new.priority:
new.next = self.head
self.head = new
else:
temp = self.head
while temp.next is not None and temp.next.priority < new.priority:
temp = temp.next
new.next = temp.next
temp.next = new
def pop(self):
if self.isEmpty():
return
temp = self.head
self.head = self.head.next
return temp
def isEmpty(self):
return self.head is None