-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathqueue.py
45 lines (32 loc) · 1.04 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
class Queue:
"""
A queue is a linear data type that follows First-In First-Out (FIFO) pattern.
There is already a built-in queue data type in python, however for
demonstration purpose, we create a Queue data type in python using a list.
The basic methods in queue are:
- length
- enqueue
- dequeue
"""
def __init__(self):
self.__data = []
def __str__(self) -> str:
return f"Queue({self.__data})"
@property
def length(self):
return self.__data.__len__()
def enqueue(self, data):
self.__data.insert(0, data)
def dequeue(self):
return self.__data.pop()
if __name__ == "__main__":
queue = Queue()
queue.enqueue(1) # 1
queue.enqueue(2) # 2, 1
queue.enqueue(3) # 3,2,1
queue.enqueue(4) # 4,3,2,1
print(f"Queue: {queue}")
print(f"length of queue: {queue.length}")
print(f"dequeue: {queue.dequeue()}") # 1 is popped
print(f"dequeue: {queue.dequeue()}") # 2 is popped
print(f"dequeue: {queue.dequeue()}") # 3 is popped