A linear queue in an array has one embarrassing bug: after a few dequeues, there is free space at the front, but rear has hit the end, so the queue says "overflow" while being half empty. Circular queue fixes this with one operator: %. Let us write it, run it, and trace it the way an exam question expects.
Disclosure: I help run GTU Hero (link at the end). The code below is my own practice implementation, compiled with gcc -Wall and run before posting.
The idea in one picture
Think of the array as a clock face with 5 slots. When rear reaches slot 4, the next slot is not 5, it is 0. In code:
rear = (rear + 1) % SIZE; Same for front. That is literally the whole trick. Baaki sab bookkeeping hai.
The full program
#include <stdio.h> #define SIZE 5 int q[SIZE]; int front = -1, rear = -1; int isFull(void) { return (rear + 1) % SIZE == front; } int isEmpty(void) { return front == -1; } void enqueue(int x) { if (isFull()) { printf("Overflow, %d not added\n", x); return; } if (isEmpty()) front = 0; rear = (rear + 1) % SIZE; q[rear] = x; } int dequeue(void) { if (isEmpty()) { printf("Underflow\n"); return -1; } int x = q[front]; if (front == rear) front = rear = -1; /* last element removed */ else front = (front + 1) % SIZE; return x; } void display(void) { if (isEmpty()) { printf("Queue empty\n"); return; } int i = front; while (1) { printf("%d ", q[i]); if (i == rear) break; i = (i + 1) % SIZE; } printf("(front=%d rear=%d)\n", front, rear); } int main(void) { for (int v = 10; v <= 50; v += 10) enqueue(v); display(); enqueue(60); printf("Removed %d\n", dequeue()); printf("Removed %d\n", dequeue()); enqueue(60); enqueue(70); display(); return 0; } Actual output on my machine:
10 20 30 40 50 (front=0 rear=4) Overflow, 60 not added Removed 10 Removed 20 30 40 50 60 70 (front=2 rear=1) Tracing it step by step
| Operation | front | rear | Array slots 0..4 |
|---|---|---|---|
| start | -1 | -1 | _ _ _ _ _ |
| enqueue 10 | 0 | 0 | 10 _ _ _ _ |
| enqueue 20, 30, 40, 50 | 0 | 4 | 10 20 30 40 50 |
| enqueue 60 | 0 | 4 | overflow, since (4+1)%5 = 0 = front |
| dequeue (10) | 1 | 4 | _ 20 30 40 50 |
| dequeue (20) | 2 | 4 | _ _ 30 40 50 |
| enqueue 60 | 2 | 0 | 60 _ 30 40 50 |
| enqueue 70 | 2 | 1 | 60 70 30 40 50 |
The last two rows are the whole point. rear wrapped from 4 to 0 to 1, reusing the slots that dequeue freed. A linear queue would have refused both inserts.
Why these exact conditions
Empty: front == -1. We reset both pointers to -1 when the last element leaves. Some textbooks use front == rear with one slot always left empty instead. Both are correct, just do not mix them in one answer.
Full: (rear + 1) % SIZE == front. If moving rear one step forward would land on front, there is no free slot. With this version all 5 slots are usable, as the trace shows.
Single element removal: when front == rear before a dequeue, the queue becomes empty, so we reset to -1. Forget this and your isEmpty() never returns true again. Classic viva question.
Complexity
Enqueue and dequeue are both O(1). No shifting, unlike the "move everything left" fix for a linear queue, which is O(n) per dequeue.
What about Python?
In Python you would normally reach for collections.deque(maxlen=n), but for the exam, write the array version: same % logic, with raise OverflowError and raise IndexError in place of the printf lines. Examiners want to see the pointer arithmetic, not a library call.
Practise
Write the trace table for this sequence yourself with SIZE 4: enqueue 1, 2, 3, dequeue, dequeue, enqueue 4, 5, 6, 7. (Hint: one of those enqueues must overflow.) Then check old questions on the GTU data structures papers page, which has 10 papers under codes 4330704 and DI03000021, all 70 marks. Search them for queue and stack programs, and ek baar haath se likh lo.
