//Coda CIRCOLARE semplice
public class Coda {
  private int head, tail;
  private int[] elem;
  private final int MAX;
  private static final int MAXELEMENTI = 10;
    
  //costruttori
  public Coda() {
    this(MAXELEMENTI);
  }
  
  public Coda(int maxelem) {
    MAX = maxelem;
    elem = new int[MAX];
    Clear();
  }
  
  //metodi canonici
  public boolean IsEmpty() {return head == tail;}
  public boolean IsFull() {return head == (tail + 1) % MAX;}
  public void Clear() {head = tail = 0;}
  
  public void EnQueque(int val) {
    if (IsFull())
      throw new CodaFullException();
    elem[tail] = val;
    tail = (tail + 1) % MAX;
  }
  
  public int DeQueque() {
    if (IsEmpty())
      throw new CodaEmptyException();
    int val = elem[head];
    head = (head + 1) % MAX;
    return val;
  }
  
  public int HeadElem() {
    return elem[head];
  }
}

//Eccezioni personalizzate per la classe Coda.
class CodaFullException extends RuntimeException {
  public CodaFullException() {
    super("Coda Piena.");
  }
  public CodaFullException(String msg) {
    super(msg);
  }
  
}

class CodaEmptyException extends RuntimeException {
  public CodaEmptyException() {
    super("Coda Vuota.");
  }
  public CodaEmptyException(String msg) {
    super(msg);
  }
  
}