# Printing will be handled in the main function so don't use print in the class
class Queue:
# Constructor
def __init__(self, capacity):
self.front = self.size = 0 # Front --> Start
self.rear = capacity - 1 # Rear --> End
self.Q = [None]*capacity
self.capacity = capacity
# if size = capacity then queue is full
def is_full(self):
return self.size == self.capacity
# Check empty
def is_empty(self):
return self.size == 0
# Enque
def enque(self, item):
if self.is_full():
return False
else:
self.rear = (self.rear + 1) % (self.capacity)
self.Q[self.rear] = item
self.size += 1
return True
# Dequeue
def dequeue(self):
if self.is_empty():
return False
else:
item = self.Q[self.front]
self.front = (self.front + 1) % (self.capacity)
self.size -= 1
return item
# Replace any item
def replace(self, item, val):
if item not in self.Q:
return False
else:
pos = self.Q.index(item)
self.Q[pos] = val
return True
# Print the queue
def __str__(self):
return str(self.Q[self.front: self.rear+1])
# Driver program -- Main
def main():
size = input("Enter the queue size : ")
if not size.isdigit():
print("ERROR!! - Wrong Input")
else:
q = Queue(int(size))
ch = int(input("-------[ Select Option ]--------\n1. Enqueue \n2. Dequeue \n3. Replace \n4.Print \n5.Exit\n============\n"))
while ch != 5:
# Switch statements
# Case 1
if ch == 1:
item = input("Enter the value:")
if q.enque(item) == True:
print("EnQueued")
else:
print("Queue is full")
# Case 2
elif ch == 2:
item=q.dequeue()
if item:
print(item," - is Dequeued")
else:
print("Queue is Empty")
# Case 3
elif ch == 3:
item = input("Enter the item to be replaced : ")
val = input("Enter the value to be replaced : ")
if q.replace(item,val):
print("Success")
else:
print("Item not found")
# Case 4
elif ch == 4:
print(q)
# Case 5
elif ch == 5:
print("Logging out...")
return
# Default
else :
print("Wrong Input")
# Next iteration
ch = int(input("-------[ Select Option ]--------\n1. Enqueue \n2. Dequeue \n3. Replace \n4.Print \n5.Exit\n============\n"))
if __name__ == '__main__':
main()
OUTPUT
Enter the queue size : 3
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
1
Enter the value:4
EnQueued
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
1
Enter the value:5
EnQueued
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
1
Enter the value:6
EnQueued
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
4
['4', '5', '6']
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
2
4 - is Dequeued
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
2
5 - is Dequeued
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
2
6 - is Dequeued
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
2
Queue is Empty
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
1
Enter the value:8
EnQueued
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
3
Enter the item to be replaced : 8
Enter the value to be replaced : 9
Success
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
4
['9']
-------[ Select Option ]--------
1. Enqueue
2. Dequeue
3. Replace
4.Print
5.Exit
============
5
Comments
Post a Comment