Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 1 · syllabusAsked 2× in KTU papersChecked by code · teacher review pending

Circular queues

A queue whose array wraps around, so slots freed at the front can be reused.

Syllabus says: “Circular Queues”

On this page · 18 sections
ormakkurippu ✎
Never-forget card

Wrap around with % MAX. FULL: (rear + 1) % MAX == front. EMPTY: front == −1.

Picture it

A merry-go-round at the Thrissur Pooram fair: after the last seat comes the first one again, so no seat is ever wasted.

Write this in the exam

A circular queue treats the array as circular, advancing rear and front with (i + 1) % MAX so that freed positions at the beginning can be reused.

Learning objectives

  • Use modulo arithmetic to wrap the front and rear indices.
  • Write insert and delete for a circular queue (KTU May 2026 Q11a, model paper Q9b).
  • State the empty and full conditions and compute the number of elements.

Prerequisites

The linear queue lesson; the % (mod) operator.

Introduction

Picture the array bent into a ring, so that index MAX − 1 is followed by index 0. When rear reaches the end it simply continues at the start, as long as that slot is free. No space is wasted.

Definition

Definition

A circular queue is an array-based queue in which the position after index MAX − 1 is index 0. Both front and rear advance with i ← (i + 1) mod MAX.

Algorithm

  1. Empty: front = rear = −1.
  2. Full: (rear + 1) mod MAX = front — the slot after rear is the front.
  3. Insert(x): if full, overflow. If empty, front = rear = 0; otherwise rear ← (rear + 1) mod MAX. Then q[rear] ← x.
  4. Delete(): if empty, underflow. item ← q[front]. If front = rear, set front = rear = −1; otherwise front ← (front + 1) mod MAX. Return item.

Number of elements (non-empty queue) = (rear − front + MAX) mod MAX + 1

Exam tipTwo textbook conventions

(1) This page: front = rear = −1 when empty; all MAX slots are usable. (2) Horowitz: front = rear = 0 initially, and front points one position before the first element. Empty is front = rear, and full is (rear + 1) mod MAX = front, so one slot always stays empty and only MAX − 1 elements fit. Both are correct; state which one you use.

Where it is used

  • Ring buffers for audio and video streaming.
  • Keyboard buffers in operating systems.
  • Round-robin CPU scheduling.

Worked example

Worked example: MAX = 4 — rear wraps from 3 to 0checked by code
OperationResultfront, rearArray (index 0 … 3)Front → rear
insert(10)ok0, 0[10 _ _ _]10
insert(20)ok0, 1[10 20 _ _]10 20
insert(30)ok0, 2[10 20 30 _]10 20 30
insert(40)ok0, 3[10 20 30 40]10 20 30 40
insert(50)OVERFLOW0, 3[10 20 30 40]10 20 30 40
deleteremoved 101, 3[_ 20 30 40]20 30 40
deleteremoved 202, 3[_ _ 30 40]30 40
insert(50)ok2, 0[50 _ 30 40]30 40 50
insert(60)ok2, 1[50 60 30 40]30 40 50 60
insert(70)OVERFLOW2, 1[50 60 30 40]30 40 50 60
deleteremoved 303, 1[50 60 _ 40]40 50 60

MAX = 4. “_” marks a slot that holds no live element.

Now watch it run

Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶.

Loading visualizer…

Algorithm / pseudocode

Circular queue (array)
  1. 1front ← −1; rear ← −1
  2. 2Insert(x):
  3. 3if (rear + 1) mod MAX = front then
  4. 4print "Queue overflow"; return
  5. 5if front = −1 then front ← 0; rear ← 0
  6. 6else rear ← (rear + 1) mod MAX
  7. 7q[rear] ← x
  8. 8Delete():
  9. 9if front = −1 then
  10. 10print "Queue underflow"; return
  11. 11item ← q[front]
  12. 12if front = rear then front ← −1; rear ← −1
  13. 13else front ← (front + 1) mod MAX
  14. 14return item
  15. 15end

C implementation

circular_queue.ctested ✓ 200/200 cases
C
/* Circular queue using an array — KTU PCCST303, Module 1
 * front = rear = -1 means empty; all MAX slots are usable.
 * Full when (rear + 1) % MAX == front.
 * Input : MAX (1..100), then commands until EOF:  INSERT x | DELETE      */
#include <stdio.h>
#include <string.h>

#define CAP 100
int q[CAP];
int front = -1, rear = -1;
int MAX;

void insert(int x) {
    if ((rear + 1) % MAX == front) { printf("OVERFLOW\n"); return; }
    if (front == -1) { front = 0; rear = 0; }
    else rear = (rear + 1) % MAX;
    q[rear] = x;
    printf("INSERTED %d\n", x);
}

void delete(void) {
    if (front == -1) { printf("UNDERFLOW\n"); return; }
    int item = q[front];
    if (front == rear) { front = -1; rear = -1; }
    else front = (front + 1) % MAX;
    printf("DELETED %d\n", item);
}

int main(void) {
    char cmd[16];
    int x;
    if (scanf("%d", &MAX) != 1 || MAX < 1 || MAX > CAP) return 1;
    while (scanf("%15s", cmd) == 1) {
        if (strcmp(cmd, "INSERT") == 0) { if (scanf("%d", &x) != 1) return 1; insert(x); }
        else if (strcmp(cmd, "DELETE") == 0) delete();
        else return 1;
    }
    printf("STATE front=%d rear=%d\nCONTENTS:", front, rear);
    if (front != -1)
        for (int i = front; ; i = (i + 1) % MAX) {      /* walk front -> rear */
            printf(" %d", q[i]);
            if (i == rear) break;
        }
    printf("\n");
    return 0;
}
Sample run · MAX = 3 · real output from gcc

Input (stdin)

3
INSERT 10
INSERT 20
INSERT 30
DELETE
INSERT 40
INSERT 50

Output (stdout)

INSERTED 10
INSERTED 20
INSERTED 30
DELETED 10
INSERTED 40
OVERFLOW
STATE front=1 rear=0
CONTENTS: 20 30 40

Open in the Code Lab →

Complexity

Insert and delete are O(1); every slot of the array is reused.

Edge cases

  • MAX = 1: after one insert, (0 + 1) mod 1 = 0 = front, so the queue is full.
  • The full test must come before the ‘first insert’ test. When the queue is empty, (−1 + 1) mod MAX = 0 ≠ −1, so an empty queue is never reported as full.
  • After wrap-around, front > rear is normal (e.g. front = 2, rear = 0).

Common mistakes

  • Using rear = MAX − 1 as the full test (that is the linear queue).
  • Writing rear = rear + 1 % MAX — precedence makes this rear + (1 % MAX). Use (rear + 1) % MAX.
  • Thinking front ≤ rear always holds.

Asked in KTU exams (2)

Quick check — can you get them all?

Question 1

Q1.MAX = 4, front = 2, rear = 1. Is the queue full?

Question 2

Q2.MAX = 5, rear = 4. After one successful insert, rear = ?

Question 3

Q3.Main advantage over a linear queue?

Practice

Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.

Exam-ready summary

  • Wrap with (i + 1) mod MAX.
  • Empty: front = −1. Full: (rear + 1) mod MAX = front.
  • Count = (rear − front + MAX) mod MAX + 1.
  • O(1) operations, no wasted space.

References

  • Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — Ch. 3 (Stacks and Queues) §3.2 (circular queues)
  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §10.1 (Stacks and queues) (ENQUEUE/DEQUEUE with wrap-around)

Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.

Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)