Enqueue Using Linked List in DSA C - Time & Space Complexity
We want to understand how the time to add an item to a linked list queue grows as the queue gets bigger.
How does the enqueue operation's time change when the queue size increases?
Analyze the time complexity of the following code snippet.
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void enqueue(struct Node** front, struct Node** rear, int value) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*rear == NULL) {
*front = *rear = newNode;
return;
}
(*rear)->next = newNode;
*rear = newNode;
}
This code adds a new element to the end of a linked list queue, updating front and rear pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and linking it at the end.
- How many times: This happens once per enqueue call, no loops or recursion inside.
Each enqueue adds one node by directly linking at the rear pointer without searching.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 main linking operation |
| 100 | About 1 main linking operation |
| 1000 | About 1 main linking operation |
Pattern observation: The time stays roughly the same no matter how big the queue is.
Time Complexity: O(1)
This means adding an item takes the same short time regardless of queue size.
[X] Wrong: "Enqueue takes longer as the queue grows because we must find the end each time."
[OK] Correct: We keep a rear pointer, so we add directly without searching, keeping time constant.
Knowing how to keep enqueue fast by using a rear pointer shows you understand efficient queue design, a key skill in many coding tasks.
"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 queue implemented using a linked list?Solution
Step 1: Understand enqueue operation
Enqueue means adding a new element to the queue.Step 2: Identify position of insertion in linked list queue
In a linked list queue, new elements are added at the rear (end) to maintain order.Final Answer:
Adds an element at the end (rear) of the queue -> Option BQuick Check:
Enqueue = Add at rear [OK]
- Confusing enqueue with dequeue operation
- Thinking enqueue adds at the front
- Mixing queue with stack operations
Solution
Step 1: Allocate memory for new node
In C, use malloc with sizeof(struct Node) to allocate memory.Step 2: Correct pointer declaration and casting
Declare pointer as struct Node* and cast malloc result to (struct Node*).Final Answer:
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); -> Option DQuick Check:
Correct malloc syntax = struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); [OK]
- Missing struct keyword in declaration
- Not casting malloc result
- Using wrong variable types
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
// Enqueue 10
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 10;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
// Enqueue 20
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 20;
newNode->next = NULL;
rear->next = newNode;
rear = newNode;
Solution
Step 1: Enqueue 10 when queue is empty
Both front and rear point to new node with data 10.Step 2: Enqueue 20 when queue has one element
rear->next points to new node with data 20, rear moves to this node.Final Answer:
10 -> 20 -> NULL -> Option CQuick Check:
Order of enqueue = 10 then 20 [OK]
- Reversing order of nodes
- Not updating rear pointer
- Forgetting to set next to NULL
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (front == NULL) {
front = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}Solution
Step 1: Check rear pointer update when queue is empty
When front is NULL, only front = newNode is executed, rear remains uninitialized.Step 2: No rear update in if branch
The else block sets rear = newNode, but it is skipped when queue is empty.Final Answer:
rear pointer is not updated when queue is empty -> Option AQuick Check:
rear must be set when queue is empty [OK]
- Not setting rear when queue is empty
- Incorrect next pointer assignment
- Wrong malloc size or casting
Solution
Step 1: Understand enqueue adds at rear
Enqueue adds new nodes at the end of the queue to maintain order.Step 2: Add 35 and then 45 at the rear
After adding 35, queue becomes 5 -> 15 -> 25 -> 35 -> NULL. Then adding 45 results in 5 -> 15 -> 25 -> 35 -> 45 -> NULL.Final Answer:
5 -> 15 -> 25 -> 35 -> 45 -> NULL -> Option AQuick Check:
Enqueue order preserved at rear [OK]
- Adding new nodes at front instead of rear
- Reversing the order of elements
- Not updating rear pointer after enqueue
Ready to prove it?
Pass the test and this pattern moves unknown
