Beginners Guide To Queues In Python
In queues, the elements are added at one end and removed from the other end.
It follows First in first out (FIFO)
wherein queues on both sides are open the element enters from one end and leaves from the other.
Ex:
In a crowded place, people who come first are served first. Queue works in a similar way.
Understanding stacks will give a better understanding about queues. You can refer to stacks basics here.
Important functions in a queue:
- To add an item to the queue is
Enqueue
. - To remove an item is known as
Dequeue
.
Operations in queues:
- Two pointers were called
Front
andRear
are used to keep track of the first and last elements in the queues. - When initialising we set the front and the rear to -1.
- On Enqueueing we increase the value of the rear index and point the rear index to the new element.
- On dequeuing, we return the value pointed by the front and increase the front index.
- Before enqueueing we check if the queue is already full.
- Before dequeuing, we check if the queue is empty.
- When dequeuing the last element we reset the value of enqueue and dequeue to -1.
Main Queue Operations
- enqueue(int data): Inserts an element at the end of the queue
- int dequeue(): Removes and returns the element at the front of the queue
Auxiliary Queue Operations
- int front(): Returns the element at the front without removing it
- int size(): Returns the number of elements stored in the queue
- int IsEmpty(): Returns True if the queue is empty and False if it's not.
Implementation
— Using the Linked list
class Node :def __init__(self, data) :
self.data = data
self.next = Noneclass Queue: def __init__(self): #initializing the head and tail to None
self.head = None
self.tail = None
self.count = 0
def enqueue(self,data):
newNode = Node(data)
if self.head is None:
self.head =newNode
else:
self.tail.next = newNode
self.tail = newNode
self.count = self.count+1
def dequeue(self):
if self.head is None:
# print("Queue is Empty")
return -1
data = self.head.data
self.head = self.head.next
self.count = self.count - 1
return data
def isEmpty(self):
return self.size() == 0
def size(self):
return self.count
def front(self):
if self.head is None:
return -1
data = self.head.data
return data
— Using the Python Lists
We use the append()
function to input elements to the list and pop()
to dequeue elements out of the list.
#Inbuilt implementation of Queue using List
queue = []
# Adding elements to the queue
#Using the .append() function
queue.append('1')
queue.append('2')
queue.append('3')
print("Initial Queue:")
print(queue)
#Queue after appending the elements
# Removing elements from the queue
print("\nElements dequeued from queue:")
#Removing first element from queueprint(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("\nQueue after removing elements:")
print(queue)
Implementation of Inbuilt Queue in Python
- The queue is a built-in module of Python which is used to implement a queue.
queue.Queue(maxsize)
initializes a variable to a maximum size of maxsize.- This Queue follows the FIFO rule.
There are various functions available in this module:
maxsize
– Returns the maximum number of items allowed in the queue.empty()
— Returns True if the queue is empty, otherwise, it returns False.get()
— Remove and return an item from the queue.put(item)
— Put an item into the queue.qsize()
— Return the number of items currently present in the queue.
from queue import Queue
#setting the size of the queue to 3
q = Queue(maxsize=3)
#adding elements to the queue using put
q.put(3)
q.put(4)
q.put(6)
#returns a boolean if the queue is full or not.
Queue.full(q)
#printing element that is popped using the get command
print(q.get())
The jupyter book to this blog is here.
Follow me to learn Machine learning from scratch and data structures in python.