Algorithm
Circular Queue Algorithm:
- Initialize the queue with a fixed size, and set
front
andrear
to -1. - Enqueue Operation:
- Check if the queue is full:
(rear + 1) % size == front
. - If full, display an overflow message and exit.
- If empty, set
front = rear = 0
. - Otherwise, update
rear = (rear + 1) % size
and insert the element atqueue[rear]
.
- Check if the queue is full:
- Dequeue Operation:
- Check if the queue is empty:
front == -1
. - If empty, display an underflow message and exit.
- Retrieve the element at
queue[front]
. - If
front == rear
, setfront = rear = -1
(queue becomes empty). - Otherwise, update
front = (front + 1) % size
.
- Check if the queue is empty:
- Display Operation:
- Check if the queue is empty:
front == -1
. - If empty, display a message indicating the queue is empty.
- Otherwise, traverse the queue from
front
torear
using modulo arithmetic to handle wrapping.
- Check if the queue is empty:
Example:

Fig. 1: Circular Queue Operations
Note: Circular queues are efficient for managing fixed-size buffers and are widely used in scenarios like task scheduling and buffering data streams.