Data Structures in C/C++

Circular Queue in C and C++

First in, first out without shifting: a circular buffer reuses free slots by wrapping its indexes around.

  • Intermediate
  • 4 min read
  • 2 runnable examples
What you'll learn
  • Enqueue and dequeue on a ring buffer
  • Wrapping indexes with %
  • Using std::queue in C++
Ring buffer with capacity 5
9[0]
·[1]
·[2]
7[3] head
8[4]
↑ dequeue returns 7
9 was added after 8 and wrapped around to slot 0.

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.

From a ring of slots to code
queue->head📍 The marker on the oldest job
(queue->head + queue->size) % 4➕ The next free slot, wrapping around
pop(&queue, &value)🖨️ Take the oldest job: 7
queue->size == 4🚫 All four slots are taken
First in, first out, with positions that wrap around.
In one sentence

A 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

  1. head is the index of the front item and size is how many items are stored.
  2. push writes at (queue->head + queue->size) % 4, wrapping to 0 after index 3.
  3. pop reads at head and moves it forward with % 4, so 7 comes out first.
  4. In C++, std::queue offers push, front and pop without 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

C
Run code →
main.c
#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);
}
Get a Run button for this example Result of running the code
7
C++
Run code →
main.cpp
#include <iostream>
#include <queue>

int main() {
    std::queue<int> values;
    values.push(7);
    values.push(9);
    std::cout << values.front() << '\n';
    values.pop();
}
Get a Run button for this example Result of running the code
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.

C

Structures and operations are usually separate and allocation is explicit.

C++

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.

Problem
int tail = queue->head + queue->size;
Fix
int tail = (queue->head + queue->size) % 4;

Removing by shifting every element

Shifting makes each removal slow; move head instead.

Problem
for (int i = 1; i < size; i++) data[i - 1] = data[i];
Fix
queue->head = (queue->head + 1) % 4;

Finished this lesson?

Mark it as learned to track your progress.

Next: Hash Table →

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