3.2.7 · HinglishLinear Data Structures

Queue — FIFO semantics, enqueue - dequeue, circular array implementation

1,896 words9 min readRead in English

3.2.7 · Coding › Linear Data Structures


Queue KYA hai?

Isko Stack (LIFO) se compare karo: stack sabse recent ko remove karta hai; queue sabse purane ko remove karta hai.

Operation Stack (LIFO) Queue (FIFO)
add push (top) enqueue (rear)
remove pop (top) dequeue (front)
kaun pehle niklega newest oldest

FIFO kyun important hai?


Implement kaise karte hain: naïve array (aur ye kyun bura hai)

Idea: ek plain array mein front index aur rear index rakho.

  • enqueue: rear pe likho, phir rear++.
  • dequeue: front pe padho, phir front++.

Kaise: Circular Array (Ring Buffer)

Wrap-around modulo se hota hai: jahaan capacity hai.

Figure — Queue — FIFO semantics, enqueue - dequeue, circular array implementation

Layout ko first principles se derive karna

Hum arr[N] store karte hain, plus ek front index aur ek count (elements ki sankhya). count kyun, rear index ki jagah? Kyunki sirf front aur rear indices se "full" aur "empty" ek jaisa dikhta hai (dono front == rear dete hain). count track karna is ambiguity ko saaf hata deta hai.

  • Queue ka Front arr[front] pe rehta hai.
  • Rear (next free slot) derive hota hai: .

Reference implementation

class CircularQueue:
    def __init__(self, N):
        self.arr = [None] * N
        self.N = N
        self.front = 0
        self.count = 0
 
    def is_empty(self): return self.count == 0
    def is_full(self):  return self.count == self.N
 
    def enqueue(self, x):
        if self.is_full(): raise OverflowError("queue full")
        rear = (self.front + self.count) % self.N   # WHY: next free slot
        self.arr[rear] = x
        self.count += 1
 
    def dequeue(self):
        if self.is_empty(): raise IndexError("queue empty")
        x = self.arr[self.front]
        self.front = (self.front + 1) % self.N       # WHY: wrap front forward
        self.count -= 1
        return x
 
    def peek(self):
        if self.is_empty(): raise IndexError("queue empty")
        return self.arr[self.front]

Chaaron core ops O(1) time mein, O(N) space mein hain. Shifting kabhi nahi.


Worked Examples


Common Mistakes (Steel-manned)


Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho bachche ice cream ke liye line mein lag rahe hain. Line mein pehla bachcha pehle ice cream paata hai aur chala jaata hai; naye bachche end mein aate hain. Ye ek queue hai. Ab maano line seedhi ki jagah circle mein kursiyon ki bani ho — jab tum last kursi tak pahuncho, to agla bachcha pehli kursi pe baith jaata hai (agar woh khali ho). Woh circle trick ka matlab hai ki tum kabhi "end se bahar nahi jaate." Hum sirf yaad rakhte hain kaun si kursi pe front wala bachcha hai aur kitne bachche baithe hain.


Active Recall

Queue kaun sa discipline follow karta hai?
FIFO — First In, First Out (sabse purana element pehle niklega).
Queue mein enqueue aur dequeue kahaan hote hain?
Enqueue rear/back pe; dequeue front se.
Naïve (non-circular) array queue space-inefficient kyun hai?
front aur rear daayein taraf drift karte hain; jab rear capacity tak pahunche toh queue "full" lagta hai chahe left slots khali hon.
Circular queue mein wrap-around kaun sa math operation enable karta hai?
Modulo: index = (index + 1) % N.
Front aur count use karke next free (rear) slot ka formula?
rear = (front + count) % N.
Dequeue ke baad naya front kaise compute karte hain?
front = (front + 1) % N.
Sirf front aur rear indices ki jagah count kyun track karte hain?
Kyunki front == rear ambiguous hai (dono empty aur full ka matlab deta hai); count empty (0) vs full (N) ko unambiguous banata hai.
Circular array mein enqueue aur dequeue ki time complexity?
O(1) each — sirf indices move hoti hain, shifting nahi.
Queue empty ki condition (count ke saath)?
count == 0.
Queue full ki condition (count ke saath, capacity N)?
count == N.
Queue vs Stack: pehle kaun remove hota hai?
Queue oldest ko remove karta hai (FIFO); Stack newest ko remove karta hai (LIFO).
Kaun sa traversal queue use karta hai: BFS ya DFS?
BFS (Breadth-First Search) queue use karta hai; DFS stack use karta hai.

Connections

  • Stack — LIFO (mirror image: newest-first vs oldest-first)
  • Modular Arithmetic (woh % N jo ring banata hai)
  • Breadth-First Search (BFS) (queue ka canonical user)
  • Deque — Double-Ended Queue (generalization: dono ends pe add/remove)
  • Circular Buffer / Ring Buffer (OS aur networking mein same structure)
  • Amortized Analysis (linked-list queue se compare karo, koi resize cost nahi)
  • Priority Queue (FIFO tod deta hai: arrival ki jagah priority se order)

Concept Map

adds at rear

removes at front

contrasts with

guarantees

used by

indices drift right

fixed by

wraps via

tracks

removes ambiguity

derives

Queue FIFO

enqueue x

dequeue

Stack LIFO

Fairness in arrival order

BFS, CPU scheduling, buffers

Naive array with front and rear

Wasted space, false full

Circular array ring buffer

next i = i+1 mod N

front plus count

full vs empty distinct

rear = front+count mod N