-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.py
More file actions
65 lines (54 loc) · 1.56 KB
/
Copy pathpriority_queue.py
File metadata and controls
65 lines (54 loc) · 1.56 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class PriorityQueueNode:
def __init__(self, val, p, n):
self.val = val
self.next = n
self.prev = p
class PriorityQueue:
def __init__(self):
self.head = None
self.len = 0
def push(self, val):
self.len += 1
if self.head is None:
self.head = PriorityQueueNode(val, None, None)
else:
for i in self:
if val <= i.val:
if i.prev is None:
n = PriorityQueueNode(val, None, i)
i.prev = n
self.head = n
else:
i.prev.next = PriorityQueueNode(val, i.prev, i)
i.prev = i.prev.next
break
elif i.next is None:
i.next = PriorityQueueNode(val, i, None)
def pop(self):
if self.head is None:
self.len = 0
return None
else:
i = self.head.val
self.head = self.head.next
if self.head is not None:
self.head.prev = None
self.len -= 1
return i
def to_array(self):
a = []
for i in self:
a.append(i.val)
return a
def __iter__(self):
self.i = self.head
return self
def __next__(self):
if self.i is None:
raise StopIteration
else:
i = self.i
self.i = self.i.next
return i
def __repr__(self):
return repr(self.to_array())