Past exam question: 2077 Chaitra (Regular), Q3
Open in the Padhaiko appQuestion
What is queue? What are different operations that can be performed over queue?
Solution
Definition of Queue: A queue is an ordered linear data structure governed by the First-In, First-Out (FIFO) policy: new items are inserted strictly at the rear end, and existing items are removed strictly from the front end.
Fundamental Queue Operations:
init(Q)/create(): Initializes an empty queue (front = 0, rear = -1orfront = rear = -1).isEmpty(Q): Returnstrueif no elements remain in the queue (used to guard against underflow).isFull(Q): Returnstruewhen fixed-size storage cannot accept more items (used to guard against overflow).enqueue(Q, x): Checks for overflow; incrementsrear(wrapping moduloMAXin circular queues) and insertsxatQ[rear].dequeue(Q): Checks for underflow; readsQ[front], advancesfront, and returns the removed item.peek(Q)/front(Q): ReturnsQ[front]without removing it.traverse(Q)/display(Q): Visits all items sequentially fromfronttorear.
Answer:
A queue is a FIFO linear data structure (enqueue at rear, dequeue at front). Its core operations are init, isEmpty, isFull, enqueue (insert at rear), dequeue (remove from front), peek (inspect front), and traverse.