CS With NehalCS With Nehal

Circular Queue (ADT) Simulator

Demonstrate wrap-around movement of front and rear pointers.

Capacity
Queue Properties
Count: 3Capacity: 6Front: 0Rear: 2

Algorithm

Current line is highlighted.

Initial circular queue loaded.
CircularQueueconst anglePerSegment = 360 / total;A0FrontB1C2Rearempty3empty4empty5
Animation Speed — 900ms

About This Simulation

This circular queue simulator shows a ring buffer with enqueue, dequeue and peek, the front and rear pointers wrapping around, and when overflow and underflow happen.

Features

  • Enqueue, dequeue and peek on a circular array
  • Front and rear pointers moving around the ring
  • Overflow and underflow detection
  • Pan, zoom and playback of every operation

Explanation

A circular queue is a queue stored in a fixed-size array where the last position is connected back to the first. When the rear reaches the end of the array it wraps around to index 0, so space freed by dequeues is reused.

How it works

  1. Keep front, rear and a count (or one empty slot) to tell full from empty.
  2. Enqueue: if the queue is full, report overflow. Otherwise set rear = (rear + 1) % capacity and store the value there.
  3. Dequeue: if the queue is empty, report underflow. Otherwise read the value at front and set front = (front + 1) % capacity.
  4. The queue is empty when count = 0 and full when count = capacity.

Why it matters

Circular buffers are used for keyboard and network buffers, audio and video streaming, producer-consumer problems and round robin scheduling. They give a queue with fixed memory and O(1) operations.

Frequently Asked Questions

How do you check if a circular queue is full?

If you keep a count, the queue is full when count = capacity. Without a count, a common method is to leave one slot empty: the queue is full when (rear + 1) % capacity = front.

Why is modulo used in a circular queue?

The modulo operator makes the index wrap around: (index + 1) % capacity goes back to 0 after the last position, which turns the array into a ring.

What is the advantage of a circular queue over a linear queue?

A linear array queue cannot reuse the slots before the front, so it can report full while space is free. A circular queue reuses those slots.

What is the time complexity of circular queue operations?

Enqueue, dequeue and peek all take O(1) time.

Topics covered

  • Circular Queue
  • Ring Buffer
  • Modulo Arithmetic
  • Front and Rear Pointers
  • Queue Overflow and Underflow

Tags

  • enqueue
  • dequeue
  • modulo

Related Simulations