-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_Test.py
134 lines (103 loc) · 3.39 KB
/
queue_Test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 == 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)
class UnlimitedPriorityQueue:
def __init__(self):
self.queues = {}
def Push(self, value, priority = 1):
if priority not in self.queues:
self.queues[priority] = Queue()
self.queues[priority].Push(value)
def Pop(self, highest_priority = 1):
for i in range(0, len(self.queues)):
i = i + 1
if ((len(self.queues[i]) > 0) and (highest_priority <= i)):
return self.queues[i].Pop()
def __len__ (self):
length = 0
for i in range(0, len(self.queues)):
length += len(self.queues[i+1])
return length
def testQueue():
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 testPriorityQueue():
pqueue = PriorityQueue()
pqueue.Push(3)
pqueue.Push(2, 2) #Medium priority ,will be displayed second
pqueue.Push(1, 1) #Top Priority, will be displayed first
print("Length is : " + str(len(pqueue)))
print(pqueue.Pop())
print(pqueue.Pop())
print(pqueue.Pop())
def testUnlimitedPriorityQueue():
upqueue = UnlimitedPriorityQueue()
upqueue.Push(3, 3)
upqueue.Push(2, 2)
upqueue.Push(1, 1)
# print("Length is : " + str(len(upqueue)))
print(upqueue.Pop())
print(upqueue.Pop())
print(upqueue.Pop())
testUnlimitedPriorityQueue()