Implementation of Queue using Linked 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 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:
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())
Output
10 20 30 False 2 20 30 True
Explanation
1. 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 Complexity
Operation | Time Complexity | Explanation |
---|---|---|
Enqueue | O(1) | Adding an element to the rear takes constant time. |
Dequeue | O(1) | Removing the front element takes constant time. |
Peek (getFront) | O(1) | Viewing the front element without removing it takes constant time. |
GetRear | O(1) | Viewing the rear element takes constant time. |
Check if Empty | O(1) | Checking if the queue is empty is a constant-time operation. |
Size | O(1) | Returning the size of the queue takes constant time. |
Implementation of Queue using Linked List in Python - FAQs
What is a queue in data structures?
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. It is similar to a real-life queue, like a line at a ticket counter or a printer queue, where the first person or task in line gets processed first.
Why use a linked list for a queue?
A linked list allows dynamic memory allocation, avoiding the fixed size of arrays and providing efficient O(1) operations for both enqueue and dequeue.
How do I implement a circular queue with a linked list?
In a circular queue, the rear node points back to the front node, and the front and rear pointers wrap around when they reach the end.
What happens when we try to dequeue from an empty queue?
If we try to dequeue an element from an empty queue:
- The dequeue() method will return None, indicating there are no elements to remove.
- The front pointer will remain None, and the rear pointer will also be None since the queue is empty.