0
0
Data Structures Theoryknowledge~10 mins

Circular queue in Data Structures Theory - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Circular queue
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)
Dequeue: Check if queue is empty?
YesReject dequeue
No
Remove element at front index
Update front index (front = (front + 1) % size)
Repeat enqueue/dequeue as needed
A circular queue uses a fixed-size array with front and rear pointers that wrap around to reuse empty space, avoiding wasted slots.
Execution Sample
Data Structures Theory
size = 5
queue = [None]*size
front = 0
rear = 0
count = 0
# Enqueue 3 elements
for x in [10,20,30]:
  if count < size:
    queue[rear] = x
    rear = (rear + 1) % size
    count += 1
This code enqueues three elements into an empty circular queue of size 5.
Analysis Table
StepOperationfrontrearcountQueue StateAction
1Initialize queue000[None, None, None, None, None]Queue is empty
2Enqueue 10011[10, None, None, None, None]Added 10 at index 0
3Enqueue 20022[10, 20, None, None, None]Added 20 at index 1
4Enqueue 30033[10, 20, 30, None, None]Added 30 at index 2
5Dequeue132[10, 20, 30, None, None]Removed 10 from index 0
6Enqueue 40143[10, 20, 30, 40, None]Added 40 at index 3
7Enqueue 50104[10, 20, 30, 40, 50]Added 50 at index 4 (wrap around)
8Enqueue 60104[10, 20, 30, 40, 50]Queue full, cannot enqueue 60
9Dequeue203[10, 20, 30, 40, 50]Removed 20 from index 1
10Enqueue 60214[60, 20, 30, 40, 50]Added 60 at index 0 (wrap around)
11Dequeue313[60, 20, 30, 40, 50]Removed 30 from index 2
12Dequeue412[60, 20, 30, 40, 50]Removed 40 from index 3
13Dequeue011[60, 20, 30, 40, 50]Removed 50 from index 4 (wrap around)
14Dequeue110[60, 20, 30, 40, 50]Removed 60 from index 0
15Dequeue110[60, 20, 30, 40, 50]Queue empty, cannot dequeue
16End110[60, 20, 30, 40, 50]No more operations
💡 Queue is empty at step 15, no more dequeues possible
State 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
front000011112234011
rear012334000111111
count012323443432100
Key Insights - 3 Insights
Why does the rear index wrap back to 0 after reaching the end of the array?
Because the queue is circular, the rear index uses modulo operation (rear = (rear + 1) % size) to wrap around and reuse empty slots at the start of the array, as shown in steps 7 and 10.
Why can't we enqueue when count equals the size even if there are None slots in the array?
Count tracks the number of elements. When count equals size, the queue is full, so enqueue is rejected (step 8), even if the array has None values from previous dequeues, because those slots are logically occupied or waiting to be overwritten.
Why does the front index move forward after a dequeue?
After removing the element at front, front moves forward using modulo to point to the next element, as in step 5 and onwards, ensuring the queue correctly tracks the current front.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 7. What is the rear index value after enqueueing 50?
A0
B4
C3
D1
💡 Hint
Check the 'rear' column at step 7 in the execution_table.
At which step does the queue become full, preventing further enqueues?
AStep 7
BStep 8
CStep 6
DStep 9
💡 Hint
Look for the step where enqueue is rejected due to full queue in the 'Action' column.
According to variable_tracker, what is the count value after step 10?
A3
B2
C4
D1
💡 Hint
Refer to the 'count' row and 'After Step 10' column in variable_tracker.
Concept Snapshot
Circular Queue:
- Fixed size array with front and rear pointers
- rear and front wrap around using modulo
- Enqueue adds at rear if not full
- Dequeue removes from front if not empty
- Count tracks number of elements
- Efficient use of space by reusing slots
Full Transcript
A circular queue is a data structure that uses a fixed-size array and two pointers, front and rear, to manage elements. The rear pointer moves forward when adding elements, and the front pointer moves forward when removing elements. Both pointers wrap around to the start of the array when they reach the end, using modulo arithmetic. This wrapping allows the queue to reuse empty spaces created by dequeues, avoiding wasted space. The count variable keeps track of how many elements are currently in the queue, preventing overflows and underflows. Enqueue operations add elements at the rear if the queue is not full, while dequeue operations remove elements from the front if the queue is not empty. This structure is efficient for fixed-size buffering scenarios.