-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasicqueue.py
More file actions
67 lines (59 loc) · 1.89 KB
/
basicqueue.py
File metadata and controls
67 lines (59 loc) · 1.89 KB
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
# Implement Queue using python List
# Invokation: python3 basicqueue.py
from typing import List
from os import system
class Queue:
def __init__(self, arr: List[int]) -> None:
self.queue = arr
def enqueue(self, x: int) -> None:
self.queue.append(x)
def dequeue(self) -> int:
if(len(self.queue) > 0):
deleted_val = self.queue.pop(0)
return deleted_val
else:
print("Queue is empty!!")
def print_queue(self) -> None:
if(len(self.queue) > 0):
queue_list = list(map(str, self.queue))
print("Front [", " <- ".join(queue_list), "] Rear")
else:
print("Queue is empty!!")
try:
N = int(input("Enter number of elements: "))
print("Enter the elements (front to rear) as space separated integers:")
l = list(map(int, input().split()))
obj = Queue(l)
print("Queue Created!")
input('\nPress any key to continue...')
while(1):
system('clear')
print("Choices:")
print("1. Enqueue")
print("2. Dequeue")
print("3. Print Queue")
print("4. Exit")
x = int(input("Enter your choice: "))
if(x == 1):
system('clear')
v = int(input("Enter value to insert: "))
obj.enqueue(v)
print(f'{v} inserted in queue.')
input('\nPress any key to continue...')
elif(x == 2):
system('clear')
v = obj.dequeue()
print(f'{v} deleted from queue.')
input('\nPress any key to continue...')
elif(x == 3):
system('clear')
obj.print_queue()
input('\nPress any key to continue...')
elif(x == 4):
print("\nExiting...")
break
else:
print("\nInvalid Input. Exiting...")
break
except:
print("\nInvalid Input. Exiting...")