Home C Queues Circular Queue C Circular Queue Definition
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.
enqueue: data[(head + count) % CAP] = v; count++;
dequeue: v = data[head]; head = (head + 1) % CAP; count--; Reference
Cheat Sheet Keep these facts handy while you work with Circular Queue in C.
Topic Detail Full count == CAPEmpty count == 0Next slot (head + count) % CAPAdvance head (head + 1) % CAP
Code
C Examples Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Deque (Double-Ended Queue) in C .
1. Wrap around the array Example 1 — Wrap around the array Capacity 3: dequeue one, then enqueue into the freed slot.
#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;
} dequeued 1
enqueue 4: ok
contents: 2 3 4
enqueue 5: full How It Works After dequeue, head is 1 and count is 2. Value 4 goes to slot (1 + 2) % 3 = 0, wrapping to the start. 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.
Remember
Key Takeaways Ring buffer.
Use modulo.
Track count.
One line: A circular queue treats the array as a ring.
Frequently Asked Questions What is a circular queue? A queue implemented on an array whose end connects back to the start, so freed slots are reused.
How do you tell if a circular queue is full? When the element count equals the capacity (or when tail+1 equals head modulo capacity in the one-empty-slot design).
What should I learn after Circular Queue? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion