0
0
DSA Pythonprogramming

Queue Implementation Using Array in DSA Python

Choose your learning style9 modes available
Mental Model
A queue is like a line where the first person in is the first person out. Using an array, we keep track of where the line starts and ends.
Analogy: Imagine a line of people waiting to buy tickets. The first person to get in line is the first to get the ticket and leave the line. The array holds the people, and two markers show who is first and last.
front -> [ ] [ ] [ ] [ ] [ ] ← rear
indexes  0   1   2   3   4
Dry Run Walkthrough
Input: Start with empty queue of size 5, enqueue 10, enqueue 20, dequeue, enqueue 30
Goal: Show how elements enter and leave the queue using array indexes
Step 1: enqueue 10 at rear
front=0, rear=0, array=[10, _, _, _, _]
Why: Add first element at start of queue
Step 2: enqueue 20 at rear+1
front=0, rear=1, array=[10, 20, _, _, _]
Why: Add next element behind the first
Step 3: dequeue element at front
front=1, rear=1, array=[10, 20, _, _, _]
Why: Remove first element by moving front forward
Step 4: enqueue 30 at rear+1
front=1, rear=2, array=[10, 20, 30, _, _]
Why: Add new element behind current rear
Result:
front=1, rear=2, array=[10, 20, 30, _, _]
Queue elements: 20 -> 30 -> null
Annotated Code
DSA Python
class Queue:
    def __init__(self, size):
        self.size = size
        self.queue = [None] * size
        self.front = -1
        self.rear = -1

    def is_empty(self):
        return self.front == -1

    def is_full(self):
        return self.rear == self.size - 1

    def enqueue(self, value):
        if self.is_full():
            print("Queue is full")
            return
        if self.is_empty():
            self.front = 0
        self.rear += 1  # move rear forward to add new element
        self.queue[self.rear] = value

    def dequeue(self):
        if self.is_empty():
            print("Queue is empty")
            return None
        value = self.queue[self.front]
        if self.front == self.rear:  # only one element
            self.front = -1
            self.rear = -1
        else:
            self.front += 1  # move front forward to remove element
        return value

    def display(self):
        if self.is_empty():
            print("Queue is empty")
            return
        result = []
        for i in range(self.front, self.rear + 1):
            result.append(str(self.queue[i]))
        print(" -> ".join(result) + " -> null")

# Driver code
q = Queue(5)
q.enqueue(10)
q.enqueue(20)
q.dequeue()
q.enqueue(30)
q.display()
if self.is_full():
check if queue has no space left to add
if self.is_empty():
initialize front when adding first element
self.rear += 1 # move rear forward to add new element
advance rear pointer to next free slot
if self.is_empty():
check if queue is empty before removing
if self.front == self.rear: # only one element
reset queue when last element is removed
self.front += 1 # move front forward to remove element
advance front pointer to next element
for i in range(self.front, self.rear + 1):
iterate from front to rear to display queue
OutputSuccess
20 -> 30 -> null
Complexity Analysis
Time: O(1) for enqueue and dequeue because we only move pointers without shifting elements
Space: O(n) because we use an array of fixed size n to store elements
vs Alternative: Compared to linked list queue, array queue uses fixed space and can be faster but may waste space if not circular
Edge Cases
enqueue when queue is full
prints 'Queue is full' and does not add element
DSA Python
if self.is_full():
dequeue when queue is empty
prints 'Queue is empty' and returns None
DSA Python
if self.is_empty():
dequeue last element
resets front and rear to -1 to mark empty queue
DSA Python
if self.front == self.rear:
When to Use This Pattern
When you need a first-in-first-out order and want fast add/remove at ends, use a queue with front and rear pointers to track positions in an array.
Common Mistakes
Mistake: Not updating front pointer on dequeue, causing repeated removal of same element
Fix: Advance front pointer by one after removing element
Mistake: Not resetting front and rear when queue becomes empty after dequeue
Fix: Set front and rear to -1 when last element is removed
Mistake: Trying to enqueue without checking if queue is full, causing index error
Fix: Check is_full before adding new element
Summary
Implements a queue using an array with front and rear pointers to add and remove elements in order.
Use when you want a simple queue with fixed size and fast enqueue/dequeue operations.
Remember to move front and rear pointers correctly and reset them when queue is empty.