-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.py
100 lines (59 loc) · 1.92 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
90
91
92
93
94
95
96
97
#this function deals with inserting an element into the Queue
def enqueue(que,ele):
if isempty(que):
front=0 #declares front and rear to be zero in the first time of enqueue
rear=0
else:
rear=len(que)-1
que.append(ele)
rear+=1
print(ele,"is inserted sucessfully!!!")
#this function deals with removing an element from the Queue
def dequeue(que):
if isempty(que):
print("Queue is empty--> Underflow")
else:
front=0
item=que.pop(front)
print("the item dequeued is:",item)
#this function displays the queue in LIFO format.
def display(que):
if isempty(que):
print("Queue is empty--> Underflow")
else:
front=0
rear=len(que)-1
print(que[front],"<--FRONT")
for i in range(front+1,rear,1):
print(que[i])
print(que[rear],"<--REAR")
#this function checks the is empty or not.
def isempty(que):
if len(que)==0:
return 1
else:
return 0
#___main__
que=[]
front=rear=None
while 1:
print("---------------------------------------------------WELCOME TO QUEUE IMPLEMENTATION------------------------------------------------------")
print("Please enter your choice:")
print("1) ENQUEUE")
print("2) DEQUEUE")
print("3) DISPLAY")
print("4) EXIT")
ch=int(input()) #accepts user input choice
#checks for the correct choice and implement user demand for the choice.
if ch==1:
ele=input("Please enter an element to be inserted:") #takes input for the element to be inserted in the queue.
enqueue(que,ele)
elif ch==2:
dequeue(que)
elif ch==3:
display(que)
elif ch==4:
print("--------------------------THANKYOU FOR USING MY QUEUE IMPLEMENTATION-------------------------------")
break #breaks the loop for choice.
else:
print("Please enter a valid choice (1-4)")