Reading history

Queue Data Structure in C: Circular Queue

4 min read 1 views

Learn the queue data structure in C: FIFO order, enqueue and dequeue, why a circular buffer is needed, full working code, and deque and priority queues.

Picture the token system at a bank: each new token number joins the end of the waiting list, and the counter always calls the lowest waiting number next. That is a queue, and its rule is FIFO (first in, first out). Printers, network routers and operating-system schedulers all keep queues like this.

In this lesson you will learn

  • The queue operations: enqueue, dequeue, front and isEmpty
  • Why a plain array queue wastes space
  • How a circular buffer fixes it, with full C code
  • Other kinds of queue: deque and priority queue

Queue Operations

OperationWhat it doesTime
enqueue(x)Adds x at the backO(1)
dequeue()Removes and returns the front itemO(1)
front()Looks at the front itemO(1)
isEmpty()Tells whether the queue has no itemsO(1)

Why a Simple Array Is Not Enough

Suppose you store the queue in an array, add at index rear and remove at index front. Each dequeue moves front one step right, so the slots at the start are never used again. After enough operations the array looks full even when it is nearly empty. Shifting every item left after each dequeue fixes that but costs O(n).

A circular buffer solves both problems: when the index reaches the end of the array it wraps back to 0, using % CAP.

A Circular Queue in C

This queue tracks the front index and a count of items. The back position is always (front + count) % CAP.

#include <stdio.h>
#include <stdbool.h>

#define CAP 4
typedef struct { int items[CAP]; int front, count; } Queue;   /* circular buffer */

static bool enqueue(Queue *q, int v) {
    if (q->count == CAP) return false;
    q->items[(q->front + q->count) % CAP] = v;
    q->count++;
    return true;
}

static bool dequeue(Queue *q, int *out) {
    if (q->count == 0) return false;
    *out = q->items[q->front];
    q->front = (q->front + 1) % CAP;
    q->count--;
    return true;
}

int main(void) {
    Queue q = {0};
    int x = 0;
    for (int t = 101; t <= 104; t++) enqueue(&q, t);       /* tokens 101..104 */
    if (!enqueue(&q, 105)) printf("queue full, token 105 must wait\n");
    for (int i = 0; i < 2; i++)
        if (dequeue(&q, &x)) printf("served %d\n", x);
    enqueue(&q, 105); enqueue(&q, 106);                   /* wraps around the array */
    while (dequeue(&q, &x)) printf("served %d\n", x);
    return 0;
}

Output

queue full, token 105 must wait
served 101
served 102
served 103
served 104
served 105
served 106
  • The queue holds 4 tokens, so token 105 is refused at first.
  • After two tokens are served, slots 0 and 1 are free. Tokens 105 and 106 wrap around into them.
  • Tokens still come out in arrival order: 101 to 106. Every operation is O(1).
  • Keeping a count avoids the classic puzzle of telling “full” from “empty” when front == rear.

A queue can also be a linked list with a head for dequeue and a tail for enqueue; it never fills up but needs a malloc per item.

Other Kinds of Queue

TypeRuleTypical use
Simple queueFirst in, first outPrint jobs, customer service lines
Circular queueFIFO in a fixed array that wraps aroundKeyboard and network buffers
Deque (double-ended)Add or remove at both endsSliding-window problems, undo with redo
Priority queueHighest-priority item leaves firstHospital triage, Dijkstra’s shortest path

Breadth-first search explores a graph level by level using a queue, the mirror image of depth-first search with a stack.

Queue Mistakes to Avoid

  1. Forgetting to wrap. Without % CAP, the index runs past the array.
  2. Confusing full and empty. If you track only front and rear, both states can look the same; keep a count or leave one slot empty.
  3. Dequeuing an empty queue. Always check first.
  4. Shifting items on every dequeue. That turns an O(1) operation into O(n).

Practice Exercises

Exercise 1. Enqueue A, B, C, dequeue once, enqueue D. What is at the front?

Show answer

B. The queue now holds B, C, D.

Exercise 2. With CAP = 4, front = 3 and count = 2, at which index is the next item stored?

Show answer

(3 + 2) % 4 = 1.

Exercise 3. Write is_full for the circular queue.

Show answer
bool is_full(const Queue *q) { return q->count == CAP; }

Frequently Asked Questions

Is a queue FIFO or LIFO?

FIFO: first in, first out. A stack is LIFO.

What is a circular queue used for?

Fixed-size buffers that must never stop, such as audio streams, keyboard input and network packets, where memory is set aside once and reused.

How is a priority queue usually built?

With a binary heap, which gives O(log n) insert and remove of the highest-priority item.

Summary

  • A queue adds at the back and removes at the front: first in, first out.
  • A circular buffer reuses array slots with % CAP, keeping every operation O(1).
  • Track a count to tell a full queue from an empty one.
  • Deques and priority queues extend the idea for other problems.
Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *