Bird
0
0
DSA Cprogramming~10 mins

Queue Implementation Using Array in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Queue Implementation Using Array
Initialize queue array with fixed size
Set front = -1, rear = -1
Enqueue operation?
NoDequeue operation?
Check if queue full
Add element at rear
Update rear pointer
Repeat operations
This flow shows how a queue is initialized and how enqueue and dequeue operations update the front and rear pointers in an array-based queue.
Execution Sample
DSA C
int queue[5];
int front = -1, rear = -1;
// Enqueue 10
rear++;
queue[rear] = 10;
front = 0;
This code initializes a queue and enqueues the element 10 at the start.
Execution Table
StepOperationFront PointerRear PointerQueue Array StateVisual State
1Initialize queue-1-1[_, _, _, _, _]Queue is empty
2Enqueue 1000[10, _, _, _, _]10 added at index 0
3Enqueue 2001[10, 20, _, _, _]20 added at index 1
4Enqueue 3002[10, 20, 30, _, _]30 added at index 2
5Dequeue12[10, 20, 30, _, _]10 removed, front moves to index 1
6Enqueue 4013[10, 20, 30, 40, _]40 added at index 3
7Dequeue23[10, 20, 30, 40, _]20 removed, front moves to index 2
8Enqueue 5024[10, 20, 30, 40, 50]50 added at index 4
9Enqueue 60 (Fail)24[10, 20, 30, 40, 50]Queue full, cannot add 60
10Dequeue34[10, 20, 30, 40, 50]30 removed, front moves to index 3
11Dequeue44[10, 20, 30, 40, 50]40 removed, front moves to index 4
12Dequeue-1-1[_, _, _, _, _]50 removed, queue empty, front and rear reset to -1
13Dequeue (Fail)-1-1[_, _, _, _, _]Queue empty, cannot dequeue
💡 Execution stops because queue is empty and no more operations are performed.
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 13
front-10001122234-1-1
rear-10122334444-1-1
queue[_, _, _, _, _][10, _, _, _, _][10, 20, _, _, _][10, 20, 30, _, _][10, 20, 30, _, _][10, 20, 30, 40, _][10, 20, 30, 40, _][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][_, _, _, _, _][_, _, _, _, _]
Key Moments - 3 Insights
Why does the front pointer move forward on dequeue but the rear pointer only moves on enqueue?
Because front points to the element to remove next, it moves forward after removing. Rear points to the last element added, so it only moves when adding new elements. See steps 5 and 6 in execution_table.
Why do front and rear reset to -1 when the queue becomes empty?
Resetting front and rear to -1 indicates the queue is empty and ready for new operations. This happens after the last element is dequeued, as shown in step 12.
Why can't we enqueue when rear reaches the last index even if front has moved forward?
Because this simple array queue does not reuse freed space at the front (no circular queue). When rear reaches the end, the queue is considered full. See step 9 where enqueue fails.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the value of front and what element was removed?
Afront = 1, element 10 removed
Bfront = 0, element 20 removed
Cfront = 2, element 30 removed
Dfront = -1, no element removed
💡 Hint
Check the 'Front Pointer' and 'Operation' columns at step 5 in execution_table.
At which step does the queue become empty again after some dequeues?
AStep 10
BStep 12
CStep 8
DStep 13
💡 Hint
Look for when front and rear reset to -1 in execution_table.
If we try to enqueue at step 9, what happens to the rear pointer?
Arear increments by 1
Brear stays the same
Crear resets to -1
Drear moves back by 1
💡 Hint
Check the 'Rear Pointer' and 'Operation' columns at step 9 in execution_table.
Concept Snapshot
Queue using array:
- Use fixed-size array with front and rear pointers
- front = -1 and rear = -1 means empty queue
- Enqueue: increment rear, add element
- Dequeue: increment front, remove element
- Reset front and rear to -1 when queue empty
- Simple array queue does not reuse freed space (no wrap-around)
Full Transcript
This visual execution shows how a queue is implemented using an array. The queue starts empty with front and rear pointers at -1. When we add (enqueue) elements, rear moves forward and elements are placed at that index. When we remove (dequeue) elements, front moves forward to the next element. If the queue becomes empty after dequeues, both pointers reset to -1. The queue is full when rear reaches the last index, even if front has moved forward, because this simple array queue does not reuse space. The execution table tracks each step, showing pointer positions and the queue array state.