public class CircularQueue implements Queue { public static final int MAX_QUEUE_SIZE = 100; private Object[] q; private int front, rear; public CircularQueue() { q = new Object[ MAX_QUEUE_SIZE ]; front = -1; // Represents the empty queue rear = -1; // Represents the empty queue } public boolean isEmpty() { return ( front == -1 ); } private static int nextIndex(int index) { return ( index+1 ) % MAX_QUEUE_SIZE; } public boolean isFull() { return nextIndex( rear ) == front; } public void enqueue( Object o ) { // pre-condition: if ( isFull() ) throw new FullQueueException(); if ( isEmpty() ) { front = 0; } rear = nextIndex( rear ); q[ rear ] = o; } public Object dequeue() { if ( isEmpty() ) throw new EmptyQueueException(); Object result = q[front]; if ( front == rear ) { front = -1; rear = -1; } else { front = nextIndex( front ); } return result; } }