Algorithm


Circular Queue Algorithm:

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

Example:

Circular Queue 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.