Implementation of Queue using Linked List in Python Last Updated : 11 Feb, 2025 Comments Improve Suggest changes Like Article Like Report A queue is a linear data structure that follows the FIFO (First In First Out) principle. The first element added is the first one to be removed. With a queue, the least recently added item is removed first. A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. The queue can be efficiently implemented using a linked list. In this implementation, we dynamically allocate memory for each element in the queue using nodes, making it more flexible than using a fixed-size array.Key Operations in Queue:Enqueue: Add an element to the rear (end) of the queue.Dequeue: Remove an element from the front (beginning) of the queue.Peek: View the front element without removing it.Check if Empty: Check whether the queue is empty.Size: Get the number of elements in the queue.Here’s how we can implement the queue using a Linked list: Python class Node: def __init__(self, key): self.key = key self.next = None class MyQueue: def __init__(self): self.front = None self.rear = None self.size = 0 # Enqueue: Add an element to the rear of the queue def enqueue(self, x): temp = Node(x) if self.rear is None: self.front = temp self.rear = temp else: self.rear.next = temp self.rear = temp self.size += 1 # Dequeue: Remove and return the element # from the front of the queue def dequeue(self): if self.front is None: return None # Store the front node's value res = self.front.key # Move the front pointer to the next node self.front = self.front.next # If the queue becomes empty, set rear to None if self.front is None: self.rear = None self.size -= 1 return res # Peek: Return the front element without removing it def getFront(self): if self.front is None: return None return self.front.key # Get the rear element def getRear(self): if self.rear is None: return None return self.rear.key # Check if the queue is empty def isEmpty(self): return self.front is None # Get the size of the queue def getSize(self): return self.size # Example if __name__ == "__main__": queue = MyQueue() queue.enqueue(10) queue.enqueue(20) queue.enqueue(30) print(queue.dequeue()) print(queue.getFront()) print(queue.getRear()) print(queue.isEmpty()) print(queue.getSize()) print(queue.dequeue()) print(queue.dequeue()) print(queue.isEmpty()) Output10 20 30 False 2 20 30 True Explanation1. Node Class: Represents a node in the queue with a key (data) and a next pointer.2. MyQueue Class:Attributes:front: Points to the first node (front of the queue).rear: Points to the last node (rear of the queue).size: Keeps track of the current number of elements in the queue.Methods:enqueue(x): Adds an element x to the rear of the queue.dequeue(): Removes and returns the element from the front of the queue.getFront(): Returns the value of the front element.getRear(): Returns the value of the rear element.isEmpty(): Checks if the queue is empty.size(): Returns the current size of the queue.Time ComplexityOperationTime ComplexityExplanationEnqueueO(1)Adding an element to the rear takes constant time.DequeueO(1)Removing the front element takes constant time.Peek (getFront)O(1)Viewing the front element without removing it takes constant time.GetRearO(1)Viewing the rear element takes constant time.Check if EmptyO(1)Checking if the queue is empty is a constant-time operation.SizeO(1)Returning the size of the queue takes constant time. Comment More infoAdvertise with us Next Article Implementation of Queue using Linked List in Python B brijkan3mz4 Follow Improve Article Tags : Python DSA Python-DSA Practice Tags : python Similar Reads Implementation of Queue using List in Python A queue is a linear data structure that follows the FIFO (First In First Out) principle. The first element added is the first one to be removed. With a queue, the least recently added item is removed first. A good example of a queue is any queue of consumers for a resource where the consumer that ca 6 min read Implementation of XOR Linked List in Python Prerequisite: XOR Linked List An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory-efficient version of Doubly Linked List can be created using only one space for the address field with every node. This memory efficient Doub 6 min read Implementation of Queues in Multi-Threading in Python3 Implementation of Queues in Multi-Threading The Prerequisites Before reading this article, some important background reading is recommended to ensure that one is well equipped with the basic knowledge of threads as well as the basic knowledge of queues before trying to mix the two together. The offi 7 min read Queue - Linked List Implementation In this article, the Linked List implementation of the queue data structure is discussed and implemented. Print '-1' if the queue is empty.Approach: To solve the problem follow the below idea:we maintain two pointers, front and rear. The front points to the first item of the queue and rear points to 8 min read Queue Implementation in Python Queue is a linear data structure that stores items in a First In First Out (FIFO) manner. With a queue, the least recently added item is removed first. One can imagine a queue as a line of people waiting to receive something in sequential order which starts from the beginning of the line. It is an o 4 min read Like