-
Notifications
You must be signed in to change notification settings - Fork 1k
/
dynamic_queue.py
107 lines (89 loc) · 2.54 KB
/
dynamic_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
'''
Queue implementation using List in Python
'''
try:
queue = []
# For the queue to be dynamic it has been put in a loop
# While the loop is true the user gets 5 options:
# To insert element in queue,remove,
# display all elelements,reverse queue or exit
while True:
op = int(input('''
Press-->
1 to insert into queue
2 to remove from queue
3 to display values of queue
4 to reverse the exisiting queue
5 to exit '''))
if op == 1:
ele = int(input("enter elem to insert "))
queue.append(ele)
elif op == 2:
if len(queue) == 0:
print("The queue is empty, insert values if required")
else:
ele = queue.pop(0)
print("Element removed is - ", ele)
elif op == 3:
if len(queue) == 0:
print("The queue is empty, insert values if required")
else:
print(queue)
elif op == 4:
queue.reverse()
print(queue)
elif op == 5:
break
else:
print("invalid option")
except ValueError:
print("Please enter integer only")
except:
print("There's been some issue please check the data you've entered")
"""
Sample Input- Output
dynamic_queue.py
Press-->
1 to insert into queue
2 to remove from queue
3 to display values of queue
4 to reverse the exisiting queue
5 to exit 1
enter elem to insert 3
Press-->
1 to insert into queue
2 to remove from queue
3 to display values of queue
4 to reverse the exisiting queue
5 to exit 1
enter elem to insert 4
1 to insert into queue
2 to remove from queue
3 to display values of queue
4 to reverse the exisiting queue
5 to exit 1
enter elem to insert 5
Press-->
1 to insert into queue
2 to remove from queue
3 to display values of queue
4 to reverse the exisiting queue
5 to exit 4
[5, 4, 3]
Press-->
1 to insert into queue
2 to remove from queue
3 to display values of queue
4 to reverse the exisiting queue
5 to exit 2
Element removed is - 5
Press-->
1 to insert into queue
2 to remove from queue
3 to display values of queue
4 to reverse the exisiting queue
5 to exit 5
"""
"""
Time Complexity - O(n)
"""