Implementation of Circular Queue in Java with Array
A Circular Queue (also called a Ring Buffer) is a queue data structure that treats its underlying array as if it were connected end-to-end in a circle. Unlike a linear queue, once the rear pointer reaches the end of the array, it wraps back to index 0 — allowing reuse of freed slots without shifting elements. This post implements a true circular queue in Java using an array, with proper wraparound logic for both the front and rear pointers. How a Circular Queue Works Both front and rear start at -1 (empty state). Enqueue: Advance rear with wraparound: rear = (rear + 1) % size. The queue is full when (rear + 1) % size == front. Dequeue: Advance front with wraparound: front = (front + 1) % size. Reset both to -1 when the last element is removed. Circular property: Elements can be added to index 0 again after front has moved past it.