Queues and circular queues
A queue is first in, first out: in at the rear, out at the front; a circular queue wraps the indexes round with (rear + 1) % MAX, and is empty when front == rear and full when (rear + 1) % MAX == front.
Print jobs, message queues, the nodes waiting their turn in a breadth-first search: all are handled first come, first served (FIFO, first in first out). A structure used that way is a queue: in at the rear, out at the front.
Array queues and "false overflow"
In an array, front points at the front element and rear at the slot after the last one (where the next one goes). Enqueue stores at data[rear] and does rear++; dequeue takes data[front] and does front++.
front↑rearAn array queue with MAX = 5 (rear points at the next free slot): enqueue a, b, c, d, then dequeue twice. What are front and rear now?
What does "false overflow" mean?
Circular queues: wrap round to 0
The fix is to join the array's two ends into a ring: after the last slot, rear goes back to 0. In code, that's %:
1rear = (rear + 1) % MAX; // after 5 comes (5 + 1) % 6 = 02front = (front + 1) % MAX;
Wrapping round brings a new problem: empty is front == rear, and full, with rear having come all the way round to front, is also front == rear. The usual fix is to leave one slot unused: when one more step of rear would reach front, it counts as full.
| Condition | Why | |
|---|---|---|
| Empty | front == rear | nothing in it |
| Full | (rear + 1) % MAX == front | one slot kept free, so full and empty look different |
| Length | (rear - front + MAX) % MAX | after wrapping, rear can be below front; adding MAX before % keeps it from going negative |
So a circular queue with MAX = 6 holds at most 5.
front↑rear1#include <stdio.h>2 3#define MAX 64 5typedef struct {6 int data[MAX];7 int front, rear;8} Queue;9 10int enqueue(Queue *q, int x) {11 if ((q->rear + 1) % MAX == q->front) return 0; // full12 q->data[q->rear] = x;13 q->rear = (q->rear + 1) % MAX;14 return 1;15}16 17int dequeue(Queue *q, int *x) {18 if (q->front == q->rear) return 0; // empty19 *x = q->data[q->front];20 q->front = (q->front + 1) % MAX;21 return 1;22}23 24int length(Queue *q) {25 return (q->rear - q->front + MAX) % MAX;26}27 28int main(void) {29 Queue q = {{0}, 0, 0};30 for (int i = 1; i <= 6; i++)31 if (!enqueue(&q, i * 10))32 printf("%d won't fit: full (length %d)\n", i * 10, length(&q));33 int x;34 dequeue(&q, &x);35 dequeue(&q, &x);36 enqueue(&q, 60);37 enqueue(&q, 70);38 printf("front = %d, rear = %d, length %d\n", q.front, q.rear, length(&q));39 while (dequeue(&q, &x))40 printf("%d ", x);41 printf("\n");42 return 0;43}
60 won't fit: full (length 5) front = 2, rear = 1, length 5 30 40 50 60 70
A circular queue with MAX = 8, front = 6, rear = 2. How many elements are in it?
1#include <stdio.h>2 3#define MAX 84 5int main(void) {6 int front = 6, rear = 2;7 printf("%d\n", (rear - front + MAX) % MAX);8 return 0;9}
A circular queue with MAX = 8 (rear points at the next free slot) has front = 3 and rear = 2. What state is it in?
MAX = 4, and 1 to 5 are enqueued into an empty queue. Fill in the "full" check so the program prints how many went in: 3.
1#include <stdio.h>2 3#define MAX 44 5int data[MAX], front = 0, rear = 0;6 7int enqueue(int x) {8 if ( == front) return 0;9 data[rear] = x;10 rear = (rear + 1) % MAX;11 return 1;12}13 14int main(void) {15 int ok = 0;16 for (int i = 1; i <= 5; i++)17 ok += enqueue(i);18 printf("%d\n", ok);19 return 0;20}
(rear + 1) % MAX == front. With MAX = 4 at most 3 fit, so the 4th and 5th are turned away.Write a circular queue
✓Finish enqueue and dequeue: return 0 when full or empty; rear and front wrap round with % as they move on.
3 2 3 5
1#include <stdio.h>2 3#define MAX 44 5typedef struct {6 int data[MAX];7 int front, rear;8} Queue;9 10int enqueue(Queue *q, int x) {11 // full: (rear + 1) % MAX == front12 return 1;13}14 15int dequeue(Queue *q, int *x) {16 // empty: front == rear17 return 1;18}19 20int main(void) {21 Queue q = {{0}, 0, 0};22 int x, ok = 0;23 for (int i = 1; i <= 4; i++)24 ok += enqueue(&q, i);25 dequeue(&q, &x);26 enqueue(&q, 5);27 printf("%d\n", ok);28 while (dequeue(&q, &x))29 printf("%d ", x);30 printf("\n");31 return 0;32}
Work out the length
✓Finish len so all three front, rear pairs below come out right (MAX = 10).
5 5 0
1#include <stdio.h>2 3#define MAX 104 5int len(int front, int rear) {6 return 0; // change to the length formula7}8 9int main(void) {10 printf("%d %d %d\n", len(2, 7), len(7, 2), len(4, 4));11 return 0;12}
Linked queues
A queue in a linked list has no false overflow: front points at the first node, rear at the last. Enqueue links after rear (tail insertion); dequeue removes front.
1// enqueue: link at the end2Node *n = new_node(x, NULL);3if (rear == NULL) front = rear = n; // it was empty4else { rear->next = n; rear = n; }5 6// dequeue: remove the first7Node *old = front;8front = front->next;9if (front == NULL) rear = NULL; // now empty: clear rear too10free(old);
front↓rear↓The last line is the one people miss: dequeuing the only node leaves front NULL, but rear still points at the node just freed.
A linked queue has one node left and you dequeue it. What happens if if (front == NULL) rear = NULL; is missing?
rear == NULL is false and changes rear->next, memory that has already been given back. On an ordinary computer this bug may go unnoticed; the engine here stops and tells you.This linked queue enqueues and dequeues a few times. What does it print at the end?
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5 int data;6 struct Node *next;7} Node;8 9Node *front = NULL, *rear = NULL;10 11void enqueue(int x) {12 Node *n = malloc(sizeof(Node));13 n->data = x;14 n->next = NULL;15 if (rear == NULL) front = rear = n;16 else { rear->next = n; rear = n; }17}18 19int dequeue(void) {20 Node *old = front;21 int x = old->data;22 front = front->next;23 if (front == NULL) rear = NULL;24 free(old);25 return x;26}27 28int main(void) {29 enqueue(1);30 enqueue(2);31 printf("%d ", dequeue());32 enqueue(3);33 printf("%d ", dequeue());34 printf("%d ", dequeue());35 enqueue(4);36 printf("%d\n", front->data);37 return 0;38}
Summary
A circular queue: wrap round with %, leave one slot free
- In at the rear, out at the front; rear points at the next free slot
- One step on: i = (i + 1) % MAX
- Empty: front == rear; full: (rear + 1) % MAX == front
- Length (rear - front + MAX) % MAX; holds at most MAX − 1
1q->data[q->rear] = x;2q->rear = (q->rear + 1) % MAX;3*x = q->data[q->front];4q->front = (q->front + 1) % MAX;