<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">public class CircularQueue implements Queue {
  
  private Object[] c;
  private int head, tail;
  int size;

  // elements are c[head,..,tail] in a circular way.
  // if size is 0, the queue is empty
  // if size &gt; 0, c[head] is the head element, c[tail] is the tail.


  public CircularQueue(int capacity) {    
    c = new Object[capacity];
    size = 0;
    head = 0;  
    tail = capacity -1;
  }
  
  /** Add o to the queue. 
   *  Precondition: isFull() returns false. */
  public void enqueue(Object o) {
    tail = (tail+1) % c.length;
    c[tail] = o;
    size++;
  }
  
  /** Return the front element.
   *  Precondition: size() != 0 */
  public Object peek() {
    return c[head];    
  }
  
  /** Remove and return the front element.
   *  Precondition: size() != 0 */
  public Object dequeue() {
    Object h = c[head];
    head = (head+1) % c.length;
    size--;
    return h;
  }
  
  /** Return the current size of this queue. 
    */
  public int size() {
    return size;
  }
  
  /** Return whether the queue is full or not. */
  public boolean isFull() {
    return size &gt;= c.length;
  }  
   
}</pre></body></html>