-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_queue.py
97 lines (76 loc) · 2.37 KB
/
my_queue.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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class Node:
def __init__(self, value, next_node, previous_node):
self.value = value
self.next_node = next_node
self.previous_node = previous_node
class Queue:
def __init__(self):
self.head = None
self.bottom = None
self.length = 0
def push(self, value):
new_node = Node(value, None, None)
if self.head:
new_node.next_node = self.head
self.head.previous_node = new_node
if self.bottom is None:
self.bottom = new_node
self.head = new_node
self.length += 1
return None
def pop(self):
if self.bottom:
tmp = self.bottom
if tmp.previous_node:
self.bottom = tmp.previous_node
else:
self.bottom = None
self.length -= 1
return tmp.value
else:
raise ValueError('Queue has nothing to Pop!')
def length(self):
return self.length
def __len__(self):
return self.length
def peek(self):
return self.head.value
class PriorityQueue:
def __init__(self):
self.low_priority = Queue()
self.med_priority = Queue()
self.high_priority = Queue()
def push(self, value, priority=3):
if priority == 3:
self.low_priority.push(value)
if priority == 2:
self.med_priority.push(value)
if priority == 1:
self.high_priority.push(value)
def pop(self, highest_priority=1):
if (len(self.high_priority) > 0) and (highest_priority <= 1):
return self.high_priority.pop()
if (len(self.med_priority) > 0) and (highest_priority <= 2):
return self.med_priority.pop()
if (len(self.low_priority) > 0) and (highest_priority <= 3):
return self.low_priority.pop()
def __len__(self):
return len(self.low_priority) + len(self.med_priority) + len(self.high_priority)
def test_queue():
queue = Queue()
queue.push(3)
queue.push(5)
queue.push(7)
print("Length is : " + str(queue.length()))
print(queue.pop())
print(queue.pop())
print(queue.pop())
def test_priority_queue():
pqueue = PriorityQueue()
pqueue.push(3)
pqueue.push(2, 2)
pqueue.push(1, 1)
print(pqueue.pop(2))
print(pqueue.pop())
print(pqueue.pop())
test_priority_queue()