Queue Using Linked List in Java (Complete Implementation with Detailed Explanation)
Queue Using Linked List (Deep Concept Guide)
Core Idea Recap
Queue = FIFO (First In First Out)
Linked List = Nodes dynamically connected.
Queue using Linked List me:
👉 Do pointers maintain karte hain:
-
Front → First element
-
Rear → Last element
Enqueue → Rear par add
Dequeue → Front se remove
What is Queue Using Linked List?
Queue using Linked List is an implementation of the queue data structure where elements are stored dynamically in nodes connected by references instead of a fixed-size array. Each node contains a data field and a reference to the next node. The queue maintains two pointers: front (which points to the first element) and rear (which points to the last element). When an element is enqueued, a new node is created and added at the rear. When an element is dequeued, the front node is removed. This implementation allows the queue to grow dynamically without predefined size limitations.
Why Linked List Queue is Better?
In an array-based queue, once the rear pointer reaches the end of the array, no more elements can be added even if there is free space at the beginning, unless a circular queue is implemented. This creates limitations in fixed-size memory allocation. In contrast, a linked list-based queue dynamically allocates memory for each new node using heap memory. As long as system memory is available, the queue can grow without predefined size constraints. This makes linked list implementation more flexible and efficient for dynamic data handling in real-world applications like scheduling systems and buffering tasks.
Internal Working (Step-by-Step)
Let’s enqueue 10, 20, 30.
Step 1: enqueue(10)
Front → 10 → null
Rear → 10
Step 2: enqueue(20)
Front → 10 → 20 → null
Rear → 20
Step 3: enqueue(30)
Front → 10 → 20 → 30 → null
Rear → 30
Now dequeue:
Remove 10
Front → 20 → 30 → null
Technical Deep Explanation
Queue maintains:
-
Node front
-
Node rear
Each node has:
-
int data
-
Node next
Enqueue algorithm:
-
Create new node
-
If queue empty → front = rear = newNode
-
Else → rear.next = newNode
-
rear = newNode
Dequeue algorithm:
-
If empty → underflow
-
temp = front
-
front = front.next
-
If front == null → rear = null
Complete Java Implementation
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class QueueLinkedList {
private Node front, rear;
public void enqueue(int data) {
Node newNode = new Node(data);
if (rear == null) {
front = rear = newNode;
return;
}
rear.next = newNode;
rear = newNode;
}
public int dequeue() {
if (front == null) {
System.out.println("Queue Underflow");
return -1;
}
int value = front.data;
front = front.next;
if (front == null)
rear = null;
return value;
}
public int peek() {
if (front == null)
return -1;
return front.data;
}
}
Time Complexity (With Reasoning)
-
Enqueue → O(1)
Direct rear pointer change. -
Dequeue → O(1)
Direct front pointer change. -
Peek → O(1)
No traversal required.
Space Complexity
O(n)
Har element ke liye ek node allocate hota hai.
Advantages
-
Dynamic size
-
No fixed capacity
-
Efficient FIFO handling
Disadvantages
-
Extra memory for pointer
-
Slightly complex than array implementation
Array Queue vs Linked List Queue
| Feature | Array Queue | Linked List Queue |
|---|---|---|
| Size | Fixed | Dynamic |
| Memory | Contiguous | Non-contiguous |
| Overflow | Possible | Rare |
Real Life Application
-
Printer queue
-
CPU scheduling
-
BFS in Graph
-
Order processing systems
Basic Question
Agar queue me 5 → 10 → 15 enqueue kiya gaya ho
to do baar dequeue karne par kaunsa element front par bachega?
Thinking Question
Queue using linked list me rear pointer maintain na karein to kya problem hogi?
Comments
Post a Comment