We See Queues in our daily life. We see people standing in queues at railways counters, ATM counters, super market bill counters, cinema ticket counters etc. Queue follows one mechanism called FIFO (First In First Out). In same way, we can arrange the elements in memory in such a way that the first element will come first, second element will come second and the last element will come last. Since It follows FIFO rule we can't insert a data items or elements in middle or starting position. insertions should be takes place in only at the rear of the queue, similarly the deletion operation can be performed only at the Front of the queue neither from Rear from Middle. We can search an element from a Queue using the position i.e., using the index.
class Queue: def __init__(self): self.q = [] def is_empty(self): return self.q == [] def add(self, element): self.q.append(element) def delete(self): if self.is_empty(): return -1 else: return self.q.pop(0) def search(self, element): if self.is_empty(): return -1 else: try: n = self.q.index(element) return n+1 except ValueError: return -2 def display(self): return self.q
Now as you can see the Queue class is created and you can save this as a python file, let it be "queue.py" so that it can be used as a module. Now it is time to perform some operation on this.
OUTPUT:
Initially the Queue is: []
=============== Queue Operations ===============
1. Add an Element
2. Delete an Element
3. Search an Element
4. Exit
Enter your choice: 1
Enter element: AI
==================================================
The Queue is: ['AI']
==================================================
=============== Queue Operations ===============
1. Add an Element
2. Delete an Element
3. Search an Element
4. Exit
Enter your choice: 1
Enter element: Silan
==================================================
The Queue is: ['AI', 'Silan']
==================================================
=============== Queue Operations ===============
1. Add an Element
2. Delete an Element
3. Search an Element
4. Exit
Enter your choice: 1
Enter element: Software
==================================================
The Queue is: ['AI', 'Silan', 'Software']
==================================================
=============== Queue Operations ===============
1. Add an Element
2. Delete an Element
3. Search an Element
4. Exit
Enter your choice: 3
Enter the element: Software
Software is found at position: 3
==================================================
The Queue is: ['AI', 'Silan', 'Software']
==================================================
=============== Queue Operations ===============
1. Add an Element
2. Delete an Element
3. Search an Element
4. Exit
Enter your choice: 2
Software is deleted from the Queue sucessfully!
==================================================
The Queue is: ['Silan', 'Software']
==================================================
=============== Queue Operations ===============
1. Add an Element
2. Delete an Element
3. Search an Element
4. Exit
Enter your choice: 4
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc