What will be the state of the queue after these operations?
enqueue(&front, &rear, 10); enqueue(&front, &rear, 20); enqueue(&front, &rear, 30);
medium
A. 10 -> 20 -> 30 -> NULL
B. 30 -> 20 -> 10 -> NULL
C. 10 -> 30 -> 20 -> NULL
D. 20 -> 10 -> 30 -> NULL
Solution
Step 1: Trace first enqueue call
Queue is empty, so front and rear point to new node with data 10.
Step 2: Trace second and third enqueue calls
New nodes with 20 and 30 are added at rear, linked in order: 10 -> 20 -> 30 -> NULL.
Final Answer:
10 -> 20 -> 30 -> NULL -> Option A
Quick Check:
Enqueue adds at rear, order preserved = C [OK]
Hint: Enqueue adds at rear, so order is insertion order [OK]
Common Mistakes:
Confusing enqueue with push (stack behavior)
Reversing order of nodes
Not updating rear pointer correctly
4. The following dequeue function for a queue using linked list has a bug. Identify the bug:
int dequeue(Node** front, Node** rear) {
if (*front == NULL) {
printf("Queue is empty\n");
return -1;
}
Node* temp = *front;
int value = temp->data;
*front = (*front)->next;
free(temp);
return value;
}
medium
A. Does not free memory of dequeued node
B. Does not update rear when queue becomes empty
C. Returns wrong value after dequeue
D. Does not check if front is NULL before dequeue
Solution
Step 1: Analyze dequeue steps
Function removes front node and updates front pointer correctly.
Step 2: Check rear pointer update
If after dequeue front becomes NULL (queue empty), rear should also be set to NULL but this is missing.
Final Answer:
Does not update rear when queue becomes empty -> Option B
Quick Check:
Rear must be NULL if queue empty = B [OK]
Hint: Set rear to NULL when front becomes NULL after dequeue [OK]
Common Mistakes:
Forgetting to update rear on empty queue
Not freeing dequeued node memory
Returning wrong data value
5. You want to implement a function reverseQueue that reverses the elements of a queue implemented using a linked list. Which approach is the most efficient and correct?
hard
A. Use a stack to push all elements, then enqueue them back to queue
B. Traverse the linked list and swap data of front and rear nodes repeatedly
C. Dequeue all elements and enqueue them back in the same order
D. Create a new linked list and copy elements in reverse order without modifying original
Solution
Step 1: Understand reversing queue with linked list
Reversing requires changing order of elements, which is not straightforward by swapping nodes in singly linked list.
Step 2: Evaluate stack approach
Using a stack to push all elements then enqueue back reverses order efficiently and correctly.
Step 3: Check other options
Swapping data repeatedly is complex and error-prone; copying to new list wastes memory; re-enqueueing same order does nothing.
Final Answer:
Use a stack to push all elements, then enqueue them back to queue -> Option A
Quick Check:
Stack reverses order efficiently = D [OK]
Hint: Use stack to reverse queue elements easily [OK]
Common Mistakes:
Trying to swap nodes in singly linked list
Re-enqueueing without changing order
Copying to new list wastes memory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.