Queue Implementation Using Linked List in DSA C++ - Time & Space Complexity
We want to understand how the time taken by queue operations changes as the number of items grows.
How does adding or removing items affect the speed of the queue?
Analyze the time complexity of the enqueue and dequeue operations in this linked list queue.
struct Node {
int data;
Node* next;
};
class Queue {
Node* front;
Node* rear;
public:
Queue() : front(nullptr), rear(nullptr) {}
void enqueue(int x) {
Node* temp = new Node{x, nullptr};
if (!rear) {
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
void dequeue() {
if (!front) return;
Node* temp = front;
front = front->next;
if (!front) rear = nullptr;
delete temp;
}
};
This code adds items to the end and removes items from the front of the queue using a linked list.
Look for loops or repeated steps inside enqueue and dequeue.
- Primary operation: Adding or removing a single node pointer.
- How many times: Each enqueue or dequeue does this once per call, no loops inside.
Each operation works by changing a few pointers, no matter how big the queue is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 pointer change |
| 100 | 1 pointer change |
| 1000 | 1 pointer change |
Pattern observation: The work stays the same even if the queue grows larger.
Time Complexity: O(1)
This means adding or removing an item takes the same short time no matter how many items are in the queue.
[X] Wrong: "Enqueue or dequeue must take longer as the queue gets bigger because there are more items."
[OK] Correct: The linked list keeps track of front and rear, so operations only change a few pointers, not all items.
Knowing that queue operations are constant time shows you understand efficient data structure design, a key skill in coding interviews.
"What if we used a singly linked list without a rear pointer? How would the time complexity of enqueue change?"
Practice
Solution
Step 1: Understand queue size limitation in arrays
Arrays have fixed size, so queues implemented with arrays have a maximum capacity.Step 2: Linked list dynamic size
Linked lists can grow or shrink dynamically, so queues using linked lists do not have fixed capacity limits.Final Answer:
Dynamic size without fixed capacity -> Option AQuick Check:
Linked list queues grow dynamically = D [OK]
- Thinking linked lists use less memory always
- Confusing access speed advantages
- Assuming linked lists are simpler to implement
Solution
Step 1: Understand node structure for linked list
A node must contain data and a pointer to the next node.Step 2: Analyze options
struct Node { int data; Node* next; }; correctly uses a pointer to next node. struct Node { int data; Node next; }; uses next as object, which is invalid. struct Node { int data; Node* prev; }; uses prev pointer, which is for doubly linked list. struct Node { int data; int next; }; uses int for next, which is incorrect.Final Answer:
struct Node { int data; Node* next; }; -> Option AQuick Check:
Node with data and next pointer = A [OK]
- Using object instead of pointer for next
- Confusing singly and doubly linked list nodes
- Using int instead of pointer for next
enqueue(10); enqueue(20); dequeue(); enqueue(30); dequeue();
Print the queue elements from front to rear.
Solution
Step 1: Perform enqueue(10) and enqueue(20)
Queue becomes: 10 -> 20 -> nullStep 2: Perform dequeue()
Removes 10, queue becomes: 20 -> nullStep 3: Perform enqueue(30)
Queue becomes: 20 -> 30 -> nullStep 4: Perform dequeue()
Removes 20, queue becomes: 30 -> nullFinal Answer:
30 -> null -> Option CQuick Check:
After operations queue front is 30 [OK]
- Forgetting to remove front on dequeue
- Appending dequeue element instead of removing
- Mixing order of operations
int dequeue() {
if (front == nullptr) return -1;
Node* temp = front;
front = front->next;
int val = temp->data;
delete temp;
return val;
}Solution
Step 1: Analyze dequeue steps
Function removes front node and returns its data correctly.Step 2: Check rear pointer update
If after dequeue front becomes nullptr (empty queue), rear should also be set to nullptr but this is missing.Final Answer:
Does not update rear when queue becomes empty -> Option BQuick Check:
Rear must be nullptr if queue empty [OK]
- Ignoring rear pointer update on empty queue
- Deleting node before reading data
- Returning wrong value after deletion
reverseQueue() that reverses the elements of a queue implemented using a linked list without using extra arrays or STL containers. Which approach is best?Solution
Step 1: Understand reversing linked list nodes
Reversing links between nodes iteratively changes the queue order without extra space.Step 2: Analyze options
Use recursion to dequeue elements and enqueue them back in reverse order uses recursion but may use call stack (extra space). Traverse linked list and swap data of front and rear nodes repeatedly swaps data but is inefficient and complex. Create a new linked list and insert elements at front while dequeuing creates new list (extra space). Use two pointers to reverse links between nodes iteratively reverses links in place efficiently.Final Answer:
Use two pointers to reverse links between nodes iteratively -> Option DQuick Check:
In-place link reversal is best for linked list queue reverse [OK]
- Using extra arrays or containers
- Swapping data instead of links
- Using recursion causing extra space
Ready to prove it?
Pass the test and this pattern moves unknown
