PadhaikoAll subjects

Past exam question: 2062 Baishakh (Regular / Back), Q2(a)(ii)

Data Structure and Algorithms · 3 marks

Open in the Padhaiko app

Topic: Queues

Question

Write the algorithm of enque and dequeue operations in circular queue.

Solution

Circular Queue Enqueue and Dequeue (Q[0..MAX-1], initially front = rear = -1):

Algorithm CQ_ENQUEUE(Q, val):
1. If (rear + 1) % MAX == front:
       Print "Queue Overflow"; Return
2. If front == -1:
       front = 0; rear = 0
   Else:
       rear = (rear + 1) % MAX
3. Q[rear] = val

Algorithm CQ_DEQUEUE(Q):
1. If front == -1:
       Print "Queue Underflow"; Return -1
2. val = Q[front]
3. If front == rear:
       front = -1; rear = -1
   Else:
       front = (front + 1) % MAX
4. Return val
Answer:

CQ_ENQUEUE checks (rear + 1) % MAX == front and stores at (rear + 1) % MAX; CQ_DEQUEUE checks front == -1, retrieves Q[front], and advances (front + 1) % MAX (resetting to -1 when emptying the last item).

All past exam questions for Data Structure and Algorithms