-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.py
184 lines (151 loc) · 3.32 KB
/
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
''' Queue implementation
FILO - first in first out
Add elements on the head and remove from tail
The pointers directions from head to tail (next)
Define class of list element - Item (value, next)
Methods:
- get_next()
- get_value()
- set_next()
Define class of queus - head, tail
Methods:
- enq(value) - add new item at head
- deq() - remove item at tail
- traverse to the item before tail
- assign tail to this item
- remember to set item's next to None
- __init__()
- __str__()
Error handling:
- remove from empty Queue
'''
class Item:
def __init__(self,value):
self.value = value
self.next = None
def get_value(self):
return self.value
def get_next(self):
return self.next
def set_next(self,next):
self.next = next
# None size restricted queue
class Queue:
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
return self.head == None
def __str__(self):
if self.isEmpty():
return "The queue is empty"
else:
out = ""
item = self.head
while item != None:
out += str(item.get_value())+" "
item = item.get_next()
return out
def enq(self,value):
new_item = Item(value)
# if queue empty
if self.isEmpty():
self.head = new_item
self.tail = self.head
else:
new_item.set_next(self.head)
self.head = new_item
def deq(self):
# if empty
if self.isEmpty():
print "Queue is empty, nothing to dequeue"
# if only one element
elif self.head != None and self.head == self.tail:
item = self.head
self.head = None
self.tail = None
return item.get_value()
# if more elements
else:
item = self.head
while item.get_next() != self.tail:
item = item.get_next()
keep_item = self.tail
self.tail = item
item.set_next(None)
return keep_item.get_value()
# this is a queue with limited space
class lQueue:
def __init__(self,limit):
self.head = None
self.tail = None
self.limit = limit
self.size = 0
def isEmpty(self):
return self.head == None
def __str__(self):
if self.isEmpty():
return "Queue is empty, nothing to print"
else:
out = ""
item = self.head
while item != None:
out += str(item.get_value())+" "
item = item.get_next()
return out
def enq(self,value):
new_item = Item(value)
if self.size == self.limit:
print "Queue full, not possible to add"
return None
elif self.isEmpty():
self.head = new_item
self.tail = new_item
self.size += 1
else:
new_item.set_next(self.head)
self.head = new_item
self.size += 1
def deq(self):
if self.isEmpty():
print "Queue is empty, nothing to remove"
return None
elif self.head != None and self.head == self.tail:
item = self.head
self.head = None
self.tail = None
self.size -= 1
return item.get_value()
else:
item = self.head
while item.get_next() != self.tail:
item = item.get_next()
keep_item = self.tail
self.tail = item
item.set_next(None)
self.size -= 1
return keep_item.get_value()
if __name__ == "__main__":
# ---- tests ----
b = Queue()
b.enq(1)
b.enq(4)
b.enq(5)
print b
a = lQueue(4)
a.enq(1)
print a," size= ",a.size
a.enq(2)
print a," size= ",a.size
a.enq(4)
print a," size= ",a.size
a.enq(6)
print a," size= ",a.size
a.enq(9)
print a," size= ",a.size
c = a.deq()
print a," size= ",a.size
print c
c = a.deq()
print a," size= ",a.size
print c