Queue :
Queue is a Linear Data structure that store and removes it's element in First-In-First-Out(FIFO).
Insertation can be perform from One end and and Deletion of elements can be performed on another end.
Insertation and Deletion of elements in Queue often called as Enqueue and Dequeue.
Operations with queue :
- Enqueue : Adds an item to the Queue.
- Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed..
- Front: To Get the First / Front item from the queue.
- Rear: To Get the Last / Rear item from the queue.
Implementation of Queue : Queue can be implemented in python using the following ways :
- list
- collections.dequeue
- queue.Queue
- list[] can be used as a queue in python.
- We can use append() method for push operation. append() is used to add elements to the top / Rear of the list and pop(0) removes the element from the begining / Front of the list.
- Like Stack , len() can be use to get the size of the Queue and queur[0] gives the Front element and queue[len(queue)-1] can gives the rear element of the Queue.
Implementation of Queue using list :
# Queue using list :
queue=[]
queue.append('a')
queue.append('b')
queue.append('c')
print("Origional queue : {}".format(queue))
popedItem = queue.pop(0)
print("Dequeue Items : {}".format(popedItem))
print("After dequeue, queue changed to : {}".format(queue))
# Output :
Origional queue : ['a', 'b', 'c']
Dequeue Items : a
After dequeue, queue changed to : ['b', 'c']
Implementation of Queue using dequeue :
- Queue can also be implemented using collections.dequeue module.
- We can use append() method for push operation. append() is used to add elements to the top / Rear of the list and pop(0) removes the element from the begining / Front of the list.
- len() can be use to get the size of the Queue and queue[len(queue)-1] can gives the topmost element of the Queue.
- Dequeue is preferred over the list in the cases where we need quicker push and pop operations.
# Queue using collections.dequeue() :
from collections import dequeue
queue = dequeue()
queue.appendleft('a')
queue.appendleft('b')
queue.appendleft('c')
print("Origional queue : {}".format(queue))
popedItem = queue.pop()
print("dequeue : {}".format(popedItem))
print("After dequeue, queue changed to : {}".format(queue))
# Output :
Origional queue : dequeue(['c', 'b', 'a'])
dequeue : a
After dequeue, queue changed to : dequeue(['c', 'b'])
We can also create our own Queue Class using list or dequeue(). Below code block shows the Implementation of Queue using collections.dequeue().
# Implementation of Queue using collections.dequeue() :
from collections import dequeue
class Queue:
def __init__(self):
self.queue = dequeue()
def __repr__(self):
cur = dequeue()
for ele in self.queue:
cur.appendleft(ele)
return str(cur)
def size(self) -> int:
return len(self.queue)
def enqueue(self, val):
self.queue.appendleft(val)
def dequeue(self):
return self.queue.pop()
def rear(self):
return self.queue[len(self.queue)-1]
def front(self):
return self.queue[0]
def isempty(self) -> bool:
if self.queue == None:
return True
else:
return False
# Creating Instance of Queue :
my_queue1 = Queue()
print(type(my_queue1))
# enqueue Operations :
my_queue1.enqueue('a')
my_queue1.enqueue('b')
my_queue1.enqueue('c')
my_queue1.enqueue('d')
print("Origional queue : {}".format(my_queue1))
# dequeue Operations :
dequeueItem = my_queue1.dequeue()
print("dequeue Items :({})".format(dequeueItem))
print("After pop() queue changed to : {}".format(my_queue1))
# Get the Size of queue :
print(my_queue1.size())
# Get the Rear Element of queue :
print(my_queue1.rear())
# Get the front Element of queue :
print(my_queue1.front())
# Validate if queue is Empty ?:
print(my_queue1.isempty())
# Output :
< class '__main__.Queue' >
Origional queue : dequeue(['a', 'b', 'c', 'd'])
dequeue Items :(a)
After pop() queue changed to : dequeue(['b', 'c', 'd'])
3
b
d
False