C Circular Queue

Intermediate
6 min read
Updated: Nov 2026
1 example

Reusing Freed Slots

A circular queue treats the array as a ring. Store head and count; the next free slot is (head + count) % CAP, and dequeue advances head = (head + 1) % CAP. Freed front slots are reused, so the queue is full only when count == CAP.

Remember
enqueue: data[(head + count) % CAP] = v; count++;
dequeue: v = data[head]; head = (head + 1) % CAP; count--;

Cheat Sheet

Keep these facts handy while you work with Circular Queue in C.

TopicDetail
Fullcount == CAP
Emptycount == 0
Next slot(head + count) % CAP
Advance head(head + 1) % CAP

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Deque (Double-Ended Queue) in C.

Example 1 — Wrap around the array

Capacity 3: dequeue one, then enqueue into the freed slot.

C
#include <stdio.h>
#define CAP 3

struct CQ { int data[CAP]; int head, count; };

static int enqueue(struct CQ *q, int v) {
    if (q->count == CAP) return 0;
    q->data[(q->head + q->count) % CAP] = v;
    q->count++;
    return 1;
}
static int dequeue(struct CQ *q) {
    int v = q->data[q->head];
    q->head = (q->head + 1) % CAP;
    q->count--;
    return v;
}

int main(void) {
    struct CQ q = {{0}, 0, 0};

    for (int i = 1; i <= 3; i++) enqueue(&q, i);
    printf("dequeued %d\n", dequeue(&q));
    printf("enqueue 4: %s\n", enqueue(&q, 4) ? "ok" : "full");

    printf("contents:");
    for (int i = 0; i < q.count; i++) printf(" %d", q.data[(q.head + i) % CAP]);
    printf("\nenqueue 5: %s\n", enqueue(&q, 5) ? "ok" : "full");
    return 0;
}

How It Works

  1. After dequeue, head is 1 and count is 2.
  2. Value 4 goes to slot (1 + 2) % 3 = 0, wrapping to the start.
  3. Reading from head with modulo yields 2, 3, 4.

Edge Cases & Pitfalls

Check these before relying on Circular Queue in real programs.

ambig

Ambiguous full vs empty

With only head and tail indexes, full and empty look alike. Track count or leave one slot empty.

neg

Negative modulo

Avoid subtracting before %; add CAP first.

Key Takeaways

  • Ring buffer.
  • Use modulo.
  • Track count.

One line: A circular queue treats the array as a ring.

Frequently Asked Questions

A queue implemented on an array whose end connects back to the start, so freed slots are reused.
When the element count equals the capacity (or when tail+1 equals head modulo capacity in the one-empty-slot design).
Continue with "Deque (Double-Ended Queue) in C" next. Add and remove at either end of a circular array. The C Queues hub lists every topic in order.

Next: Deque (Double-Ended Queue) in C

Add and remove at either end of a circular array.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful