Bird
0
0
DSA Cprogramming~10 mins

Circular Queue Implementation Using Array in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Circular Queue Implementation Using Array
Initialize front = -1, rear = -1
Enqueue Operation
Check if queue is full?
Reject enqueue
If empty, set front=0
Insert element at rear
Update rear = (rear + 1) % size
Dequeue Operation
Check if queue is empty?
Reject dequeue
Remove element at front
If front == rear, reset front and rear to -1
Repeat operations as needed
This flow shows how a circular queue uses front and rear pointers to enqueue and dequeue elements in a fixed-size array, wrapping around when reaching the end.
Execution Sample
DSA C
int size = 5;
int queue[5];
int front = -1, rear = -1;
// Enqueue 10, 20, 30
// Dequeue one element
// Enqueue 40, 50, 60
This code enqueues three elements, dequeues one, then enqueues three more to show circular behavior.
Execution Table
StepOperationfrontrearQueue ArrayPointer ChangesVisual State
1Initialize queue-1-1[_, _, _, _, _]front = -1, rear = -1Empty queue
2Enqueue 1000[10, _, _, _, _]front=0 (was -1), rear=010 inserted at index 0
3Enqueue 2001[10, 20, _, _, _]rear=120 inserted at index 1
4Enqueue 3002[10, 20, 30, _, _]rear=230 inserted at index 2
5Dequeue12[10, 20, 30, _, _]front=110 removed from index 0
6Enqueue 4013[10, 20, 30, 40, _]rear=340 inserted at index 3
7Enqueue 5014[10, 20, 30, 40, 50]rear=450 inserted at index 4
8Enqueue 60 (wrap)10[60, 20, 30, 40, 50]rear=0 (wrap)60 inserted at index 0 (wrap)
9Queue Full Check10[60, 20, 30, 40, 50]front=1, rear=0Queue is full, next enqueue rejected
10Dequeue20[60, 20, 30, 40, 50]front=220 removed from index 1
11Dequeue30[60, 20, 30, 40, 50]front=330 removed from index 2
12Dequeue40[60, 20, 30, 40, 50]front=440 removed from index 3
13Dequeue00[60, 20, 30, 40, 50]front=050 removed from index 4
14Dequeue last element-1-1[60, 20, 30, 40, 50]front=-1, rear=-160 removed from index 0, queue empty
15Queue Empty Check-1-1[60, 20, 30, 40, 50]front=-1, rear=-1Queue is empty, dequeue rejected
💡 Execution stops as queue is full at step 9 and empty at step 15, no further enqueue or dequeue possible.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11After Step 12After Step 13After Step 14After Step 15
front-1000111112340-1-1
rear-1012234000000-1-1
Key Moments - 3 Insights
Why does rear wrap to 0 after reaching the end of the array?
Because the queue is circular, rear uses modulo operation (rear + 1) % size to wrap around. See step 8 in execution_table where rear moves from 4 to 0.
Why do we reset front and rear to -1 after removing the last element?
When front equals rear and we dequeue, the queue becomes empty. Resetting front and rear to -1 marks the empty state. See step 14 in execution_table.
How do we know the queue is full?
Queue is full when the next rear position equals front. At step 9, rear=0 and front=1, so next enqueue is rejected because (rear + 1) % size == front.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what is the position of rear after enqueueing 60?
A4
B0
C1
D3
💡 Hint
Check the 'rear' column at step 8 in execution_table.
At which step does the queue become empty again after multiple dequeues?
AStep 14
BStep 13
CStep 12
DStep 15
💡 Hint
Look at front and rear values resetting to -1 in variable_tracker and execution_table.
If we try to enqueue at step 9, what happens?
AElement is added at rear=1
Bfront pointer moves forward
CQueue is full, enqueue rejected
DQueue becomes empty
💡 Hint
See 'Queue Full Check' operation and 'Visual State' at step 9 in execution_table.
Concept Snapshot
Circular Queue uses a fixed-size array with front and rear pointers.
Enqueue adds at rear, dequeue removes from front.
Pointers wrap around using modulo to reuse space.
Queue is empty when front and rear are -1.
Queue is full when next rear equals front.
Reset pointers when queue becomes empty after dequeue.
Full Transcript
This visualization shows how a circular queue works using an array. We start with front and rear pointers at -1, meaning the queue is empty. When we enqueue, if the queue is empty, front is set to 0. Rear moves forward to add elements. When rear reaches the end of the array, it wraps around to 0 using modulo operation. Dequeue removes elements from front and moves front forward similarly. When the last element is removed, front and rear reset to -1 to mark empty queue. The queue is full when the next rear position equals front, preventing further enqueue. This step-by-step trace helps understand how circular queues efficiently use array space by wrapping pointers.