0
0
DSA Pythonprogramming~10 mins

Queue Implementation Using Array in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Queue Implementation Using Array
Initialize empty array queue
Enqueue: Add element at end
Check if queue is full?
YesReject enqueue
No
Dequeue: Remove element from front
Check if queue is empty?
YesReject dequeue
No
Shift front pointer forward
Repeat operations as needed
This flow shows how elements are added at the end and removed from the front of an array-based queue, with checks for full and empty states.
Execution Sample
DSA Python
queue = []
front = 0
rear = -1

# Enqueue 10
rear += 1
queue.append(10)

# Enqueue 20
rear += 1
queue.append(20)

# Dequeue
if front <= rear:
    item = queue[front]
    front += 1
This code enqueues two elements and dequeues one by moving the front pointer.
Execution Table
StepOperationQueue ArrayFront PointerRear PointerVisual State
1Initialize queue[]0-1Queue: [] Front=0 Rear=-1
2Enqueue 10[10]00Queue: [10] Front=0 Rear=0
3Enqueue 20[10, 20]01Queue: [10, 20] Front=0 Rear=1
4Dequeue (remove 10)[10, 20]11Queue: [10, 20] Front=1 Rear=1 (10 removed)
5Enqueue 30[10, 20, 30]12Queue: [10, 20, 30] Front=1 Rear=2
6Dequeue (remove 20)[10, 20, 30]22Queue: [10, 20, 30] Front=2 Rear=2 (20 removed)
7Dequeue (remove 30)[10, 20, 30]32Queue: [10, 20, 30] Front=3 Rear=2 (30 removed)
8Dequeue attempt on empty queue[10, 20, 30]32Queue empty: Front > Rear, cannot dequeue
💡 Step 8 stops because front pointer (3) is greater than rear pointer (2), meaning queue is empty.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8
queue[][10][10, 20][10, 20][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
front00011233
rear-10112222
Key Moments - 3 Insights
Why does the front pointer move forward instead of removing the element physically?
Because in array-based queue, we just move the front pointer to mark the next element as front. The old element stays but is ignored. See Step 4 and Step 6 in execution_table where front moves but array stays same.
What happens when front pointer becomes greater than rear pointer?
It means the queue is empty and no elements can be dequeued. This is shown in Step 8 where dequeue is rejected because front=3 > rear=2.
Why does rear pointer start at -1?
Rear starts at -1 to indicate the queue is empty initially. When first element is enqueued, rear moves to 0. See Step 1 and Step 2 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of front pointer after Step 5?
A0
B1
C2
D3
💡 Hint
Check the 'Front Pointer' column at Step 5 in execution_table.
At which step does the queue become empty?
AStep 6
BStep 8
CStep 7
DStep 4
💡 Hint
Look for when front pointer is greater than rear pointer in execution_table.
If we enqueue 40 after Step 7, what will be the rear pointer value?
A3
B2
C4
D5
💡 Hint
Rear pointer increments by 1 on enqueue; see rear pointer changes in variable_tracker.
Concept Snapshot
Queue using array:
- Enqueue adds element at rear (end)
- Dequeue removes element from front
- Use front and rear pointers
- Queue empty if front > rear
- Rear starts at -1, front at 0
Full Transcript
This visualization shows how a queue works using an array. We add elements at the rear end and remove from the front. The front and rear pointers track where to remove and add. Initially, the queue is empty with rear at -1 and front at 0. When we enqueue, rear moves forward and element is added. When we dequeue, front moves forward to ignore the removed element. The array keeps all elements but only those between front and rear are valid. When front pointer passes rear pointer, the queue is empty and no dequeue is possible. This simple pointer movement avoids shifting elements in the array.