Circular Queue in C and C++
First in, first out without shifting: a circular buffer reuses free slots by wrapping its indexes around.
- Enqueue and dequeue on a ring buffer
- Wrapping indexes with %
- Using std::queue in C++
Front, back and wrap-around
The problem
Print jobs, customer support tickets and network packets should be handled in the order they arrived. A queue keeps that order, and a circular buffer does it in a fixed array without shifting elements after each removal.
Think of it like this
A circular queue is like a ring of 4 numbered slots holding waiting print jobs. A marker shows the oldest job, and new jobs go into the next free slot. After slot 3, both wrap around to slot 0, so nothing ever has to be moved.
queue->head📍 The marker on the oldest job(queue->head + queue->size) % 4➕ The next free slot, wrapping aroundpop(&queue, &value)🖨️ Take the oldest job: 7queue->size == 4🚫 All four slots are takenA queue is a first-in, first-out (FIFO) collection; a circular queue stores it in a fixed array and wraps positions around with the modulo operator.
Important points
headis the index of the front item andsizeis how many items are stored.pushwrites at(queue->head + queue->size) % 4, wrapping to 0 after index 3.popreads atheadand moves it forward with% 4, so 7 comes out first.- In C++,
std::queueofferspush,frontandpopwithout managing indexes.
When to use it
- Task and message queues processed in arrival order.
- Fixed-size buffers for audio, network data or logs.
- Breadth-first search.
When not to use it
- The newest item should come out first: use a stack.
- Items have priorities: use a priority queue (
std::priority_queue).
See how it works
Enqueue and dequeue to see that items leave in the same order they arrived.
C and C++ code examples
#include <stdio.h>
typedef struct {
int data[4];
int head;
int size;
} Queue;
int push(Queue *queue, int value) {
if (queue->size == 4) return 0;
int tail = (queue->head + queue->size) % 4;
queue->data[tail] = value;
++queue->size;
return 1;
}
int pop(Queue *queue, int *value) {
if (queue->size == 0) return 0;
*value = queue->data[queue->head];
queue->head = (queue->head + 1) % 4;
--queue->size;
return 1;
}
int main(void) {
Queue queue = {{0}, 0, 0};
int value;
push(&queue, 7);
push(&queue, 9);
pop(&queue, &value);
printf("%d\n", value);
}
7
#include <iostream>
#include <queue>
int main() {
std::queue<int> values;
values.push(7);
values.push(9);
std::cout << values.front() << '\n';
values.pop();
}
7
C and C++ comparison
push places each value at the position after the current last item, using % 4 to wrap around the array. pop takes the value at head, which is the oldest one, 7, and moves head forward. No elements are shifted, so both operations take the same short time.
Structures and operations are usually separate and allocation is explicit.
Classes and containers can preserve invariants and manage resources automatically.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add empty and capacity-boundary tests.
- Implement cleanup and verify no leaks remain.
- Compare operation complexity with a standard container.
Common mistakes
Not wrapping the index
Without % 4 the tail runs past the end of the array.
int tail = queue->head + queue->size;
int tail = (queue->head + queue->size) % 4;
Removing by shifting every element
Shifting makes each removal slow; move head instead.
for (int i = 1; i < size; i++) data[i - 1] = data[i];
queue->head = (queue->head + 1) % 4;
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges