Past exam question: 2074 Bhadra (Regular), Q3
Open in the Padhaiko appQuestion
Define queue. Explain enqueue and dequeue operation with example.
Solution
Definition of Queue: A queue is a linear First-In, First-Out (FIFO) data structure in which additions happen at the rear and removals happen at the front.
Enqueue and Dequeue Operations with Example (MAX = 4):
enqueue(x): Checks overflow (rear == MAX - 1in linear queue). Incrementsrearand insertsQ[rear] = x.dequeue(): Checks underflow (front == -1 || front > rear). ReadsQ[front], advancesfront(resetting if empty), and returns the item.
Example: Starting empty (front = -1, rear = -1):
enqueue(10)[10](front=0, rear=0)enqueue(20)[10, 20](front=0, rear=1)enqueue(30)[10, 20, 30](front=0, rear=2)dequeue()removes10, leaving[20, 30](front=1, rear=2).
Answer:
A queue is a FIFO structure where enqueue(x) inserts at rear and dequeue() removes from front. Enqueuing 10, 20, 30 and dequeuing once leaves [20, 30] with 10 returned first.