Enqueue Using Linked List in DSA C++ - Time & Space Complexity
We want to understand how the time to add an item to a queue using a linked list changes as the queue grows.
How does the number of steps grow when we add more items?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
Node* next;
};
void enqueue(Node*& front, Node*& rear, int value) {
Node* newNode = new Node{value, nullptr};
if (rear == nullptr) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}
This code adds a new item to the end of a queue implemented with a linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating pointers.
- How many times: Each enqueue call does this once, no loops or recursion involved.
Adding one item always takes the same number of steps, no matter how many items are already in the queue.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 (create node + update pointers) |
| 100 | 1 (same steps as for 10) |
| 1000 | 1 (still just one set of steps) |
Pattern observation: The time to add one item stays the same regardless of queue size.
Time Complexity: O(1)
This means adding an item takes a constant amount of time, no matter how big the queue is.
[X] Wrong: "Adding an item takes longer as the queue grows because we have to find the end each time."
[OK] Correct: We keep a pointer to the end (rear), so we add directly without searching, keeping the time constant.
Knowing that enqueue in a linked list queue is constant time shows you understand efficient data structure design, a key skill in interviews.
"What if we did not keep a rear pointer and had to find the end each time? How would the time complexity change?"
Practice
enqueue operation do in a linked list implementation of a queue?Solution
Step 1: Understand enqueue operation in queue
Enqueue means adding an element to the queue.Step 2: Identify linked list position for enqueue
In a linked list queue, enqueue adds at the rear (end) to maintain FIFO order.Final Answer:
Adds an element at the end (rear) of the linked list -> Option AQuick Check:
Enqueue = Add at rear [OK]
- Confusing enqueue with dequeue operation
- Adding element at front instead of rear
- Thinking enqueue removes elements
Solution
Step 1: Check node creation syntax in C++
Correct way to create a node dynamically isNode* temp = new Node(data);.Step 2: Verify linking and updating rear pointer
Link new node byrear->next = temp;and update rear to new noderear = temp;.Final Answer:
Node* temp = new Node(data); rear->next = temp; rear = temp; -> Option CQuick Check:
Use new with pointer and update rear [OK]
- Not using pointer (*) for node
- Missing 'new' keyword for dynamic allocation
- Incorrect assignment without pointers
enqueue(10); enqueue(20); enqueue(30);
Solution
Step 1: Enqueue 10 on empty queue
Queue becomes 10 -> null.Step 2: Enqueue 20 and 30 sequentially
Adding 20 at rear: 10 -> 20 -> null; then 30 at rear: 10 -> 20 -> 30 -> null.Final Answer:
10 -> 20 -> 30 -> null -> Option BQuick Check:
FIFO order maintained: 10, 20, 30 [OK]
- Reversing order of elements
- Confusing enqueue with push front
- Ignoring null termination
void enqueue(int data) {
Node* temp = new Node(data);
if (rear == nullptr) {
front = temp;
} else {
rear->next = temp;
}
// Missing update of rear pointer here
}Solution
Step 1: Check rear pointer update after enqueue
rear pointer must point to the newly added node to keep track of queue end.Step 2: Identify missing update in code
Code missesrear = temp;after linking new node.Final Answer:
rear pointer is not updated to the new node -> Option AQuick Check:
rear must update to new node [OK]
- Forgetting to update rear pointer
- Updating front pointer incorrectly
- Setting next pointer wrongly
Solution
Step 1: Enqueue 35 at rear
Queue becomes 5 -> 15 -> 25 -> 35 -> null.Step 2: Dequeue removes front element 5
After dequeue, front moves to 15, so queue is 15 -> 25 -> 35 -> null.Final Answer:
15 -> 25 -> 35 -> null -> Option DQuick Check:
Enqueue rear, dequeue front; update pointers accordingly [OK]
- Removing wrong element on dequeue
- Not updating front pointer after dequeue
- Adding new element at front instead of rear
Ready to prove it?
Pass the test and this pattern moves unknown
