Bird
Raised Fist0
DSA Cprogramming~10 mins

Queue Implementation Using Array in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Queue Implementation Using Array
Initialize queue array with fixed size
↓
Set front = -1, rear = -1
↓
Enqueue operation?
No→Dequeue 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.

Practice

(1/5)
1. What is the main characteristic of a queue implemented using an array?
easy
A. It follows First-In-First-Out (FIFO) order
B. It follows Last-In-First-Out (LIFO) order
C. It allows random access to elements
D. It stores elements in sorted order

Solution

  1. Step 1: Understand queue behavior

    A queue is a data structure where the first element added is the first one to be removed, which is FIFO.
  2. Step 2: Compare with options

    It follows First-In-First-Out (FIFO) order correctly states FIFO order. It follows Last-In-First-Out (LIFO) order is LIFO (stack), C allows random access (array), D stores sorted (not queue).
  3. Final Answer:

    It follows First-In-First-Out (FIFO) order -> Option A
  4. Quick Check:

    Queue = FIFO [OK]
Hint: Queue always removes the oldest element first [OK]
Common Mistakes:
  • Confusing queue with stack (LIFO)
  • Thinking queue allows random access
  • Assuming queue sorts elements automatically
2. Which of the following is the correct way to declare a queue array of size 5 in C?
easy
A. queue int[5];
B. int queue[5];
C. int queue();
D. int queue = new int[5];

Solution

  1. Step 1: Recall C array declaration syntax

    In C, arrays are declared as type name[size], e.g., int queue[5];
  2. Step 2: Check each option

    int queue[5]; matches correct syntax. queue int[5]; is invalid syntax. int queue(); declares a function, not an array. int queue = new int[5]; is Java-style syntax.
  3. Final Answer:

    int queue[5]; -> Option B
  4. Quick Check:

    C array declaration = int name[size] [OK]
Hint: C arrays use type name[size] syntax [OK]
Common Mistakes:
  • Using Java or other language syntax
  • Confusing function declaration with array
  • Missing semicolon or brackets
3. Consider this C code snippet for a queue implemented using an array of size 3:
int queue[3];
int front = -1, rear = -1;

// Enqueue 10
rear++;
queue[rear] = 10;
if(front == -1) front = 0;

// Enqueue 20
rear++;
queue[rear] = 20;

// Dequeue
int val = queue[front];
front++;

// Enqueue 30
rear++;
queue[rear] = 30;

// Current queue elements from front to rear?
medium
A. 30 -> null
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> null
D. 10 -> 30 -> null

Solution

  1. Step 1: Trace enqueue and dequeue operations

    Initially front = -1, rear = -1. Enqueue 10: rear=0, queue[0]=10, front=0. Enqueue 20: rear=1, queue[1]=20. Dequeue: val=queue[front]=queue[0]=10, front=1. Enqueue 30: rear=2, queue[2]=30.
  2. Step 2: Identify current queue elements

    front=1, rear=2, so elements are queue[1]=20 and queue[2]=30. Hence queue is 20 -> 30 -> null.
  3. Final Answer:

    20 -> 30 -> null -> Option C
  4. Quick Check:

    After dequeue, front moves forward; queue = 20, 30 [OK]
Hint: After dequeue, front moves forward; queue shows elements from front to rear [OK]
Common Mistakes:
  • Not updating front after dequeue
  • Including dequeued elements in output
  • Confusing front and rear indices
4. Identify the error in this queue enqueue function using array in C:
void enqueue(int queue[], int *rear, int size, int value) {
    if (*rear == size) {
        printf("Queue is full\n");
        return;
    }
    (*rear)++;
    queue[*rear] = value;
}
medium
A. rear should be compared with size - 1, not size
B. rear should be incremented after inserting value
C. Function should return int instead of void
D. queue array should be dynamically allocated

Solution

  1. Step 1: Analyze the full condition

    The check if(*rear == size) is wrong. Array indices are 0 to size-1, so full when *rear == size-1 before incrementing. Current check allows overflow.
  2. Step 2: Compare options

    rear should be compared with size - 1, not size identifies the correct fix: compare with size-1, not size. Others are incorrect.
  3. Final Answer:

    rear should be compared with size - 1, not size -> Option A
  4. Quick Check:

    Full: *rear == size-1 [OK]
Hint: Full check: rear == size - 1 [OK]
Common Mistakes:
  • Comparing rear == size instead of size-1
  • Incorrect overflow condition
  • Ignoring zero-based indexing
5. You have a queue implemented using an array of size 4 with front = 1 and rear = 3. After dequeuing two elements and enqueuing one element, what will be the correct front and rear values if you implement a circular queue?
hard
A. front = 2, rear = 0
B. front = 3, rear = 1
C. front = 0, rear = 1
D. front = 3, rear = 0

Solution

  1. Step 1: Understand initial state and operations

    Initial: front=1, rear=3, size=4. Dequeue twice: front moves forward twice. Since circular, front = (1 + 2) % 4 = 3.
  2. Step 2: Enqueue one element

    rear moves forward by one circularly: rear = (3 + 1) % 4 = 0.
  3. Final Answer:

    front = 3, rear = 0 -> Option D
  4. Quick Check:

    Circular queue uses modulo for front and rear [OK]
Hint: Use modulo to wrap front and rear in circular queue [OK]
Common Mistakes:
  • Not using modulo for circular increment
  • Updating front or rear incorrectly
  • Confusing front and rear positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard