forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue_Using_Arrays.py
93 lines (84 loc) · 1.64 KB
/
Queue_Using_Arrays.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
size = 4
front = -1
rear = -1
"""
Uncomment to input size from user and validate it
while (1):
size = int(raw_input("Enter max input\n> "))
if size > 0:
break
"""
queue = [0] * size
#Function to check for empty queue
def isEmpty():
global front, rear
if (front == -1 or front > rear):
return True
#Fuction to insert an element in queue
def enqueue(element):
global front, rear
if rear == size - 1:
print "Queue is Full"
else:
if front == -1:
front = 0
#Uncomment to get input from user
#element = int(raw_input("Enter the element to insert\n> "))
rear = rear + 1
queue[rear] = element
print "Item inserted successfully"
#Fuction to delete an element from the queue
def dequeue():
global front, rear
if isEmpty():
print "Queue is Empty"
else:
element = queue[front]
front += 1
print "Item deleted: " + str(element)
#Fuction to display elements from queue
def display():
global front, rear
if isEmpty():
print "Queue is Empty"
else:
temp = front
while (temp <= rear):
print queue[temp],
temp += 1
print
"""
Code to get choice form the user
while(1):
print "---Queue Operations---"
print "1. Insert an element in queue."
print "2. Delelte an element from queue."
print "3. Display the current queue."
print "4. Exit."
ch = int(raw_input("Enter your choice: "))
if ch == 1:
enqueue()
elif ch == 2:
dequeue()
elif ch == 3:
display()
else:
break
"""
enqueue(23)
enqueue(45)
enqueue(65)
enqueue(89)
enqueue(90)
dequeue()
dequeue()
"""
Output
Item inserted successfully
Item inserted successfully
Item inserted successfully
Item inserted successfully
Queue is Full
Item deleted: 23
Item deleted: 45
"""