A circular queue of size 6 uses convention B (front and rear start at 0; front is one slot before the first element). Perform: enqueue A, B, C, D, E, F; dequeue twice; enqueue F, G, H. Give front, rear and the count after each step.
A queue is a FIFO list: elements enter at the rear and leave from the front. Circular queues reuse the space a linear array queue wastes, deques allow both ends, and queues drive CPU scheduling, breadth-first search, spooling and buffers.
A queue is an ordered list in which elements are inserted at one end, the rear (or tail), and removed from the other end, the front (or head). The element that has waited longest leaves first, so a queue is FIFO (First In, First Out) — equivalently LILO. Like the stack it is an abstract data type: it can be built on an array or on a linked list.
| Operation | Effect | Error condition |
|---|---|---|
| enqueue(x) / insert | add x at the rear | overflow if the queue is full |
| dequeue() / delete | remove and return the front element | underflow if the queue is empty |
| front() / peek() | return the front element without removing it | undefined when empty |
| isEmpty(), isFull(), size() | status queries | — |
Queues appear wherever requests must be served in arrival order: a ticket counter, cars at a toll plaza, documents waiting for a shared printer, calls waiting for a call-centre agent, and processes waiting for the CPU.
The simplest implementation uses an array queue[MAX] with two indices, front and rear, both −1 for an empty queue. Enqueue increments rear and stores the element (setting front to 0 for the very first element); dequeue returns queue[front] and increments front. The queue is empty when front == -1 or front > rear, and full when rear == MAX - 1.
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1; /* empty queue */
void enqueue(int x) {
if (rear == MAX - 1) { printf("[overflow: %d] ", x); return; }
if (front == -1) front = 0; /* first element */
queue[++rear] = x;
}
int dequeue(void) {
if (front == -1 || front > rear) { printf("[underflow] "); return -1; }
return queue[front++];
}
int main(void) {
for (int i = 1; i <= 5; i++) enqueue(i * 10); /* 10 20 30 40 50: full */
int a = dequeue();
int b = dequeue();
printf("removed %d %d\n", a, b); /* removed 10 20 */
enqueue(60); /* overflow, although queue[0] and queue[1] are free */
printf("\n");
return 0;
}front are free, yet rear is stuck at MAX − 1, so the next enqueue reports overflow. Shifting all elements to the front after each dequeue fixes it but costs O(n); resetting front = rear = −1 when the queue becomes empty helps only in that special case. The real fix is the circular queue.A circular queue (ring buffer) treats the array as a ring: the position after MAX − 1 is 0. Both indices advance with modular arithmetic — rear = (rear + 1) % MAX and front = (front + 1) % MAX — so cells freed at the front are reused as the rear wraps around. Because ‘front == rear’ alone cannot tell a full ring from an empty one, textbooks use one of three conventions. Learn the first two: both appear in PSC questions.
| Convention | Start | Empty when | Full when | Elements | Capacity |
|---|---|---|---|---|---|
| A — front, rear = indices of first and last elements | front = rear = −1 | front == −1 | (rear + 1) % MAX == front | (rear − front + MAX) % MAX + 1 | MAX |
| B — front = one slot before the first element, rear = last | front = rear = 0 | front == rear | (rear + 1) % MAX == front | (rear − front + MAX) % MAX | MAX − 1 (one slot always empty) |
| C — keep a count variable | front = 0, rear = MAX − 1, count = 0 | count == 0 | count == MAX | count | MAX |
#include <stdio.h>
#define MAX 5
int cq[MAX];
int front = -1, rear = -1; /* empty: front == -1 */
int isEmpty(void) { return front == -1; }
int isFull(void) { return (rear + 1) % MAX == front; }
void enqueue(int x) {
if (isFull()) { printf("[overflow: %d] ", x); return; }
if (isEmpty()) front = 0; /* first element */
rear = (rear + 1) % MAX; /* wrap around */
cq[rear] = x;
}
int dequeue(void) {
if (isEmpty()) { printf("[underflow] "); return -1; }
int x = cq[front];
if (front == rear) front = rear = -1; /* removed the only element */
else front = (front + 1) % MAX;
return x;
}
int count(void) { return isEmpty() ? 0 : (rear - front + MAX) % MAX + 1; }
int main(void) {
enqueue(10); enqueue(20); enqueue(30); enqueue(40);
int a = dequeue();
int b = dequeue();
printf("removed %d %d\n", a, b); /* removed 10 20 */
enqueue(50); enqueue(60); enqueue(70); /* 60, 70 wrap to 0, 1 */
printf("front=%d rear=%d count=%d\n", front, rear, count()); /* front=2 rear=1 count=5 */
enqueue(80); /* overflow */
printf("\n");
while (!isEmpty()) printf("%d ", dequeue()); /* 30 40 50 60 70 */
printf("\n");
return 0;
}| Operation | front | rear | Array [0 1 2 3 4] | Count |
|---|---|---|---|---|
| enqueue 10, 20, 30, 40 | 0 | 3 | 10 20 30 40 · | 4 |
| dequeue → 10 | 1 | 3 | · 20 30 40 · | 3 |
| dequeue → 20 | 2 | 3 | · · 30 40 · | 2 |
| enqueue 50 | 2 | 4 | · · 30 40 50 | 3 |
| enqueue 60 (rear wraps: (4 + 1) % 5 = 0) | 2 | 0 | 60 · 30 40 50 | 4 |
| enqueue 70 | 2 | 1 | 60 70 30 40 50 | 5 — full, since (1 + 1) % 5 == front |
| enqueue 80 | 2 | 1 | unchanged | overflow |
| dequeue → 30 | 3 | 1 | 60 70 · 40 50 | 4 |
A deque (double-ended queue, pronounced ‘deck’) allows insertion and deletion at both ends: insertFront, insertRear, deleteFront and deleteRear. It generalises both earlier structures — using only one end makes it a stack, inserting at one end and deleting at the other makes it a queue. It is usually built on a circular array or a doubly linked list so that all four operations are O(1).
| Type | Insertion | Deletion | Remember it as |
|---|---|---|---|
| Input-restricted deque (IRD) | at one end only (rear) | at both ends | the input side is restricted |
| Output-restricted deque (ORD) | at both ends | at one end only (front) | the output side is restricted |
#include <stdio.h>
#define MAX 6
int dq[MAX], front = -1, rear = -1; /* empty when front == -1 */
int isFull(void) { return (rear + 1) % MAX == front; }
int isEmpty(void) { return front == -1; }
void insertFront(int x) {
if (isFull()) return;
if (isEmpty()) front = rear = 0;
else front = (front - 1 + MAX) % MAX; /* step back with wrap-around */
dq[front] = x;
}
void insertRear(int x) {
if (isFull()) return;
if (isEmpty()) front = rear = 0;
else rear = (rear + 1) % MAX;
dq[rear] = x;
}
int deleteFront(void) {
if (isEmpty()) return -1;
int x = dq[front];
if (front == rear) front = rear = -1;
else front = (front + 1) % MAX;
return x;
}
int deleteRear(void) {
if (isEmpty()) return -1;
int x = dq[rear];
if (front == rear) front = rear = -1;
else rear = (rear - 1 + MAX) % MAX;
return x;
}
int main(void) {
insertRear(20); insertRear(30); insertFront(10); insertFront(5); /* 5 10 20 30 */
int a = deleteRear();
int b = deleteFront();
printf("%d %d\n", a, b); /* 30 5 */
while (!isEmpty()) printf("%d ", deleteFront()); /* 10 20 */
printf("\n");
return 0;
}What can restricted deques produce? Feed the input 1, 2, 3, 4 in order. The output 4 1 3 2 is possible on an input-restricted deque (insert all four at the rear, then delete rear 4, front 1, rear 3, front 2) but not on an output-restricted one, because 3 would have to sit between 1 and 2. Conversely, 4 2 1 3 is possible on an output-restricted deque (insert 1; 2 at the front; 3 at the rear; 4 at the front; then delete everything from the front) but not on an input-restricted one, since after 4 leaves, 2 is stuck in the middle of 1 2 3.
Linked queue. Keep two pointers: front to the first node and rear to the last. Enqueue appends after rear and dequeue removes the front node, both O(1) with no fixed capacity. Two details are easy to forget: enqueuing into an empty queue must set both pointers, and dequeuing the last node must reset rear to NULL as well.
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *next; };
struct Node *front = NULL, *rear = NULL;
void enqueue(int x) { /* insert at rear: O(1) */
struct Node *p = malloc(sizeof *p);
if (!p) { printf("overflow\n"); return; }
p->data = x; p->next = NULL;
if (rear == NULL) front = rear = p; /* queue was empty */
else { rear->next = p; rear = p; }
}
int dequeue(int *x) { /* delete at front: O(1), 0 on underflow */
if (front == NULL) return 0;
struct Node *p = front;
*x = p->data;
front = p->next;
if (front == NULL) rear = NULL; /* queue became empty */
free(p);
return 1;
}
int main(void) {
int x;
enqueue(5); enqueue(15); enqueue(25);
while (dequeue(&x)) printf("%d ", x); /* 5 15 25 */
printf("\n");
return 0;
}Queue from two stacks. Push every new element onto stack S1 (the ‘inbox’). To dequeue, pop from stack S2 (the ‘outbox’); if S2 is empty, first pop everything from S1 and push it onto S2, which reverses the order so the oldest element ends up on top. Each element is pushed and popped at most twice in its lifetime, so although one dequeue may move many elements, the amortised cost is O(1) per operation.
#include <stdio.h>
#define MAX 100
int s1[MAX], s2[MAX], top1 = -1, top2 = -1; /* s1: inbox, s2: outbox */
void enqueue(int x) { s1[++top1] = x; } /* always push onto s1: O(1) */
int dequeue(void) {
if (top2 == -1) /* outbox empty: pour the inbox into it */
while (top1 != -1) s2[++top2] = s1[top1--];
if (top2 == -1) { printf("[underflow] "); return -1; }
return s2[top2--]; /* oldest element is on top of s2 */
}
int main(void) {
enqueue(1); enqueue(2); enqueue(3);
printf("%d ", dequeue()); /* 1 (3, 2, 1 moved to s2) */
enqueue(4);
printf("%d ", dequeue()); /* 2 */
printf("%d ", dequeue()); /* 3 */
printf("%d\n", dequeue()); /* 4 (4 moved to s2) */
return 0;
}| Operation | S1 (inbox) | S2 (outbox) | Returned | Elements moved |
|---|---|---|---|---|
| enqueue 1, 2, 3 | 1 2 3 | empty | — | 0 |
| dequeue | empty | 3 2 | 1 | 3 (S2 was empty) |
| enqueue 4 | 4 | 3 2 | — | 0 |
| dequeue | 4 | 3 | 2 | 0 |
| dequeue | 4 | empty | 3 | 0 |
| dequeue | empty | empty | 4 | 1 |
The reverse construction — a stack from two queues — makes one operation O(n): push into q1 in O(1), and to pop, move n − 1 elements from q1 to q2, remove the last one, and swap the roles of the queues (or make push the costly operation instead).
| Structure | Insert at | Delete from | Order of service |
|---|---|---|---|
| Linear queue | rear | front | FIFO; freed front cells wasted (array version) |
| Circular queue | rear (wrapping) | front (wrapping) | FIFO; reuses freed cells |
| Deque | both ends | both ends | as the program chooses |
| Input-restricted deque | one end | both ends | — |
| Output-restricted deque | both ends | one end | — |
| Priority queue | anywhere (by priority) | highest-priority element | by priority, not arrival |
Try the problem first, then reveal the working one step at a time.
A circular queue of size 6 uses convention B (front and rear start at 0; front is one slot before the first element). Perform: enqueue A, B, C, D, E, F; dequeue twice; enqueue F, G, H. Give front, rear and the count after each step.
A circular queue of size 8 has front = 6 and rear = 3. How many elements does it hold if (a) front and rear index the first and last elements, (b) front is one position before the first element?
The input 1, 2, 3, 4, 5 arrives in this order. Decide whether the outputs 5 1 4 2 3 and 5 3 1 2 4 can be produced by an input-restricted deque (insert at rear only) and by an output-restricted deque (delete at front only).
A linear array queue has MAX = 5. You enqueue 5 elements and dequeue 2. Can a sixth element be enqueued? Explain, and say how a circular queue changes the answer.
A circular queue of size 6 uses convention B (one slot kept empty). front = 4 and rear = 1. How many elements are stored and how many more can be added?
Write enqueue and dequeue for a circular queue that uses a count variable (convention C), so that all MAX cells can be used.
In a queue built from two stacks, one dequeue can cost O(n). Show that any sequence of n enqueues and n dequeues costs O(n) in total.
Perform on an initially empty structure: insert 3, insert 7, insert 5, remove, insert 9, remove, v = remove. What is v if the structure is (a) a queue, (b) a stack?
10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.