forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.py
89 lines (71 loc) · 1.96 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
class Node:
def __init__(self, value):
self.value = value
self.left_ptr = None
@property
def prev(self):
return self.left_ptr
@prev.setter
def prev(self, ptr):
self.left_ptr = ptr
@property
def get_value(self):
return self.value
class Queue:
def __init__(self):
self.head = None
self.tail = None
self.length = 0
def enqueue(self, value):
new = Node(value)
self.length += 1
# if empty make new node both head and tail of queue
if self.head is None:
self.head = new
self.tail = new
# otherwise add new node to end of queue and update tail
else:
self.tail.prev = new
self.tail = new
def dequeue(self):
if self.length == 0:
print("Queue is empty, cannot dequeue")
return
else:
tmp = self.head
self.head = self.head.prev
self.length -= 1
return tmp.get_value
def show(self):
# first element is the head of the queue
ptr = self.head
val = []
while ptr is not None:
val.append(ptr.get_value)
ptr = ptr.prev
print(val)
def length(self):
return self.length
def main():
queue = Queue()
print("Queuing 1 and 7")
queue.enqueue(1)
queue.show()
print(f"Queue is length {queue.length}")
queue.enqueue(7)
queue.show()
print(f"Queue is length {queue.length}")
print(f"Dequeuing {queue.dequeue()}")
queue.show()
print(f"Queue is length {queue.length}")
print(f"Dequeuing {queue.dequeue()}")
queue.show()
print(f"Queue is length {queue.length}")
print(f"Dequeuing {queue.dequeue()}")
print(f"Queue is length {queue.length}")
print("Queueing 12")
queue.enqueue(12)
queue.show()
print(f"Queue is length {queue.length}")
if __name__ == "__main__":
main()