-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_linked_list.py
76 lines (62 loc) · 1.65 KB
/
queue_linked_list.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
class Node:
def __init__(self, data: any) -> None:
self.data = data
self.next = None
def __str__(self) -> str:
return str(self.data)
class Queue:
def __init__(self) -> None:
self.front = None
self.rear = None
def __len__(self) -> int:
size = 0
run = self.front
while run:
size += 1
run = run.next
return size
def __iter__(self) -> None:
run = self.front
while run:
yield run
run = run.next
def __str__(self) -> str:
if self.is_empty():
return "Queue is Empty"
else:
return '->'.join([str(node) for node in self])
def is_empty(self) -> bool:
return not bool(self.front)
def enqueue(self, data: any) -> None:
node = Node(data)
if self.is_empty():
self.front = node
self.rear = node
else:
self.rear.next = node
self.rear = node
def dequeue(self) -> any:
if not self.is_empty():
ret = self.front
self.front = ret.next
return ret.data
def get_front(self) -> any:
if not self.is_empty():
return self.front.data
def get_rear(self) -> any:
if not self.is_empty():
return self.rear.data
def main():
pass
# q = Queue()
# q.enqueue(1)
# q.enqueue(2)
# q.enqueue(3)
# print(q.dequeue())
# print(q)
# print(q.get_front())
# print(q.get_rear())
# print(len(q))
# print(q.is_empty())
if __name__ == '__main__':
main()