A Queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means that the element inserted first is removed first. A queue can be implemented using different data structures, including arrays and linked lists. When a queue is implemented using a linked list, each element is stored in a separate node containing data and a reference to the next node. Two pointers, called front and rear, are used to manage insertion and deletion operations efficiently. Unlike a fixed-size array implementation, a linked list-based queue can grow and shrink dynamically as elements are added or removed. In this article, you will learn what a queue using a linked list is, how it works, its operations, implementation using JavaScript, time complexity, advantages, disadvantages, and applications.
Table of Contents
What Is a Queue Using a Linked List?
A Queue Using a Linked List is an implementation of the queue data structure in which elements are stored in nodes connected through references.
Each node contains two parts:
- Data: Stores the actual value or element.
- Next: Stores a reference to the next node in the queue.
A linked list-based queue follows the FIFO (First In, First Out) principle. Elements are inserted at the rear and removed from the front.
Unlike an array-based queue with a fixed capacity, a linked list-based queue can dynamically allocate memory whenever a new element is inserted.
For example, when elements 10, 20, and 30 are inserted into a queue, they are stored in separate linked nodes. The element 10 is removed first because it was inserted earliest.
How Does a Queue Using a Linked List Work?
A queue using a linked list works by connecting individual nodes in the order in which elements are inserted.
The queue maintains two important pointers:
- front points to the first node.
- rear points to the last node.
The basic working process is:
- Initially, both front and rear are set to null.
- When an element is inserted, a new node is created.
- The new node is connected after the current rear node.
- The rear pointer is updated to the new node.
- When an element is removed, the front pointer moves to the next node.
- If the last element is removed, both front and rear are set to null.
This approach allows insertion and deletion at opposite ends without shifting the remaining elements.
Structure of a Node in a Queue
A node is the basic building block of a linked list-based queue. Each node contains data and a reference to the next node.
The following JavaScript class demonstrates how a node can be created.
// Create a node for a linked list-based queue.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
const node = new Node(10);
console.log(node.data);
console.log(node.next);
Output:
10
null
Explanation:
- The Node class creates a new node.
- The data property stores the element.
- The next property stores the reference to the next node.
- Initially, next is set to null because the node is not connected to another node.
Front and Rear Pointers
Front and rear pointers are used to manage the elements in a linked list-based queue.
- Front Pointer: The front pointer refers to the first node in the queue. It is used during the dequeue and peek operations
- Rear Pointer: The rear pointer refers to the last node in the queue. It is used during the enqueue operation to insert new elements efficiently.
- Empty Queue Condition: When the queue is empty, both front and rear point to null.
- Single Element Condition: When the queue contains only one node, both front and rear point to the same node.
- After Deletion: When the last remaining node is removed, both pointers must be reset to null.
Operations of Queue Using Linked List
A linked list-based queue supports several important operations.
- Enqueue Operation: The enqueue operation inserts a new element at the rear of the queue. A new node is created and connected after the current rear node. The rear pointer is then updated to the newly created node. If the queue is empty, both front and rear point to the new node.
- Dequeue Operation: The dequeue operation removes the element from the front of the queue. The front pointer moves to the next node. If the queue becomes empty after deletion, both front and rear are reset to null.
- Peek Operation: The peek operation returns the element at the front without removing it.
- IsEmpty Operation: This operation checks whether the queue is empty by checking whether the front pointer is null.
- Size Operation: This operation returns the total number of elements currently stored in the queue.
- Display Operation: This operation visits each node from front to rear and displays the stored elements.
Implementation of Queue Using Linked List in JavaScript
The following JavaScript program demonstrates how to implement a queue using a linked list. It uses a Node class to store elements and a Queue class to manage insertion, deletion, and other operations.
// Implement a queue using a linked list in JavaScript.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
this.count = 0;
}
enqueue(element) {
const newNode = new Node(element);
if (this.rear === null) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
this.count++;
return `${element} inserted`;
}
dequeue() {
if (this.isEmpty()) {
return "Queue Underflow";
}
const removedElement = this.front.data;
this.front = this.front.next;
if (this.front === null) {
this.rear = null;
}
this.count--;
return removedElement;
}
peek() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.front.data;
}
isEmpty() {
return this.front === null;
}
size() {
return this.count;
}
display() {
if (this.isEmpty()) {
return "Queue is empty";
}
let current = this.front;
const elements = [];
while (current !== null) {
elements.push(current.data);
current = current.next;
}
return elements;
}
}
const queue = new Queue();
console.log(queue.enqueue(10));
console.log(queue.enqueue(20));
console.log(queue.enqueue(30));
console.log("Queue:", queue.display());
console.log("Front Element:", queue.peek());
console.log("Removed:", queue.dequeue());
console.log("Queue After Deletion:", queue.display());
console.log("Queue Size:", queue.size());
Output:
10 inserted
20 inserted
30 inserted
Queue: [ 10, 20, 30 ]
Front Element: 10
Removed: 10
Queue After Deletion: [ 20, 30 ]
Queue Size: 2
Explanation:
- The Node class creates individual nodes containing data and a next reference.
- The Queue class initializes front and rear to null and sets the element count to zero.
- The enqueue() method creates a new node and inserts it at the rear.
- The dequeue() method removes the front node and updates the front pointer.
- If the queue becomes empty after deletion, the rear pointer is also reset to null.
- The peek() method returns the front element without removing it.
- The isEmpty() method checks whether the queue contains any elements.
- The size() method returns the current number of elements.
- The display() method traverses the linked list and collects all elements.
In this example, 10, 20, and 30 are inserted into the queue. The peek operation returns 10 because it is the first element. The dequeue operation removes 10, leaving 20 and 30 in the queue.
Time Complexity of Queue Using Linked List
The time complexity of a queue using a linked list depends on the operation being performed.
| Operation | Time Complexity |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Peek | O(1) |
| IsEmpty | O(1) |
| Size | O(1) |
| Display | O(n) |
Here, n represents the number of elements in the queue.
Enqueue and dequeue operations take O(1) time because the implementation maintains front and rear pointers. It does not need to traverse the entire linked list for insertion or deletion.
The display operation takes O(n) time because every node must be visited.
Advantages of Queue Using Linked List
A linked list-based queue provides several advantages.
- Dynamic Size: The queue can grow or shrink as elements are inserted or removed.
- Efficient Insertion: Enqueue operations take O(1) time when a rear pointer is maintained.
- Efficient Deletion: Dequeue operations take O(1) time because the front pointer can be updated directly.
- No Fixed Capacity: Unlike a fixed-size array queue, it does not require a predefined maximum capacity.
- Memory Flexibility: Memory is allocated when new nodes are created and released when nodes are no longer referenced.
Disadvantages of Queue Using Linked List
Despite its advantages, a linked list-based queue also has some limitations.
- Extra Memory: Each node requires additional memory to store a reference to the next node.
- Complex Implementation: Managing node connections and pointers requires more logic than a simple array-based queue.
- No Direct Index Access: Accessing an element at a particular position requires traversing the linked list.
- Memory Allocation Overhead: Creating and managing individual nodes can introduce additional overhead.
- Cache Performance: Nodes may be stored at different memory locations, which can result in less efficient cache usage compared with contiguous arrays.
Applications of Queue Using Linked List
Linked list-based queues are useful in applications where the number of elements changes frequently.
- CPU Scheduling: Operating systems can use queues to manage processes waiting for CPU execution. Processes can be added or removed dynamically as scheduling requirements change.
- Printer Scheduling: Printing systems can maintain a queue of documents waiting to be printed. New printing requests can be inserted at the rear and completed requests removed from the front.
- Network Communication: Network systems can use queues to temporarily store incoming data packets before they are processed or transmitted.
- Task Management: Background processing systems can use linked list-based queues to manage tasks that are waiting for execution.
- Breadth-First Search (BFS): Graph traversal algorithms use queues to store nodes waiting to be visited. A linked list implementation can support efficient insertion and deletion.
- Message Processing: Message queuing systems can store messages that need to be processed by applications or services. A queue helps maintain the order of requests.
Common Mistakes to Avoid
-
Forgetting to Update the Rear Pointer: Always update rear when a new node is inserted.
-
Ignoring the Empty Queue Condition: Check whether front is null before performing a dequeue operation.
-
Not Resetting Rear After the Last Deletion: When the final node is removed, both front and rear must be set to null.
-
Incorrect Node Connections: Make sure the current rear node points to the newly inserted node.
-
Ignoring the Size Counter: Increase the count after insertion and decrease it after deletion to maintain the correct queue size.
Frequently Asked Questions
1. What is a queue using a linked list?
A queue using a linked list is a FIFO data structure in which elements are stored in connected nodes instead of consecutive array positions.
2. Which pointers are used in a linked list-based queue?
The two main pointers are front and rear. Front points to the first node, while rear points to the last node.
3. What is the time complexity of enqueue and dequeue operations?
Both enqueue and dequeue operations take O(1) time when the queue maintains front and rear pointers.
4. What happens when the last element is removed from the queue?
When the last element is removed, both front and rear pointers are set to null to indicate that the queue is empty.
5. What is the difference between a queue using an array and a queue using a linked list?
An array-based queue stores elements in array positions and may have a fixed capacity, while a linked list-based queue stores elements in dynamically allocated nodes and can grow as needed.
Conclusion
A Queue Using a Linked List is an efficient implementation of the queue data structure that follows the FIFO principle. It uses connected nodes and front and rear pointers to manage insertion and deletion operations. Enqueue and dequeue operations can both be performed in O(1) time when the pointers are maintained correctly. Unlike a fixed-size array queue, a linked list-based queue can grow dynamically as new elements are added. Understanding this implementation helps developers choose the right data structure for applications that require flexible memory usage and efficient queue operations.
0 Comments