0
0
DSA Pythonprogramming~10 mins

Circular Queue Implementation Using Array in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Circular Queue Implementation Using Array
Initialize queue with fixed size
Enqueue: Check if queue is full?
YesReject enqueue
No
Add element at rear index
Update rear index (rear = (rear + 1) % size)
Increment count
Dequeue: Check if queue is empty?
YesReject dequeue
No
Remove element at front index
Update front index (front = (front + 1) % size)
Decrement count
Repeat enqueue/dequeue as needed
This flow shows how a circular queue uses a fixed-size array with front and rear pointers that wrap around using modulo to reuse space.
Execution Sample
DSA Python
size = 5
queue = [None]*size
front = 0
rear = 0
count = 0

# Enqueue 10
if count < size:
  queue[rear] = 10
  rear = (rear + 1) % size
  count += 1

# Enqueue 20
if count < size:
  queue[rear] = 20
  rear = (rear + 1) % size
  count += 1

# Dequeue
if count > 0:
  removed = queue[front]
  queue[front] = None
  front = (front + 1) % size
  count -= 1
This code enqueues two elements (10, 20) and then dequeues one element from a circular queue of size 5.
Execution Table
StepOperationfrontrearcountQueue StateExplanation
0Initialize queue000[None, None, None, None, None]Queue is empty, front and rear at 0
1Enqueue 10011[10, None, None, None, None]10 added at index 0, rear moves to 1
2Enqueue 20022[10, 20, None, None, None]20 added at index 1, rear moves to 2
3Dequeue121[None, 20, None, None, None]10 removed from index 0, front moves to 1
4Enqueue 30132[None, 20, 30, None, None]30 added at index 2, rear moves to 3
5Enqueue 40143[None, 20, 30, 40, None]40 added at index 3, rear moves to 4
6Enqueue 50104[None, 20, 30, 40, 50]50 added at index 4, rear wraps to 0
7Enqueue 60104[None, 20, 30, 40, 50]Queue full, enqueue rejected
8Dequeue203[None, None, 30, 40, 50]20 removed from index 1, front moves to 2
9Enqueue 60214[60, None, 30, 40, 50]60 added at index 0, rear moves to 1
10Dequeue313[60, None, None, 40, 50]30 removed from index 2, front moves to 3
11Dequeue412[60, None, None, None, 50]40 removed from index 3, front moves to 4
12Dequeue011[60, None, None, None, None]50 removed from index 4, front wraps to 0
13Dequeue110[None, None, None, None, None]60 removed from index 0, front moves to 1, queue empty
14Dequeue110[None, None, None, None, None]Queue empty, dequeue rejected
💡 Execution stops after last dequeue attempt when queue is empty (count=0).
Variable Tracker
VariableStartAfter Step 1After 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 14
front000111112234011
rear012234000111111
count012123443432100
Key Moments - 3 Insights
Why does the rear pointer wrap back to 0 after reaching the end of the array?
Because the queue is circular, rear uses modulo operation (rear = (rear + 1) % size) to wrap around. See step 6 where rear moves from 4 to 0.
Why can't we enqueue when count equals the size even if there is None space in the array?
Count tracks the number of elements. When count == size, the queue is full, so enqueue is rejected (step 7). The circular queue reuses space by wrapping pointers, not by shifting elements.
What happens when we dequeue from an empty queue?
Dequeue is rejected because count is 0 (step 14). The code checks count > 0 before dequeueing to avoid errors.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 6. What is the value of rear and why?
Arear is 0 because it wrapped around after reaching the end
Brear is 5 because it moved forward by one
Crear is 4 because it did not move
Drear is 1 because it reset to the start
💡 Hint
Check the 'rear' column at step 6 and the explanation about wrapping.
At which step does the queue become full for the first time?
AStep 5
BStep 6
CStep 7
DStep 4
💡 Hint
Look at the 'count' column and queue state to see when count equals size.
If we dequeue at step 14, what will be the front pointer value?
A1
B0
CCannot dequeue, front stays the same
D2
💡 Hint
See step 14 where dequeue is rejected because queue is empty.
Concept Snapshot
Circular Queue using array:
- Fixed size array with front, rear pointers
- Enqueue at rear, then rear = (rear + 1) % size
- Dequeue at front, then front = (front + 1) % size
- Track count to check full (count == size) or empty (count == 0)
- Wrap pointers to reuse space without shifting elements
Full Transcript
A circular queue uses a fixed-size array and two pointers: front and rear. Enqueue adds an element at rear and moves rear forward using modulo to wrap around. Dequeue removes an element from front and moves front forward similarly. A count variable tracks how many elements are in the queue to detect full or empty states. This avoids shifting elements and efficiently uses space. The execution table shows enqueue and dequeue steps, pointer movements, and queue state changes. Key moments clarify pointer wrapping, full queue detection, and empty queue handling.