//classe Pila implementata con Lista semplice di oggetti
public class LLPilaObject {
  private ListaComparable elem;
    
  //costruttore  
  public LLPilaObject() {
    elem = new ListaComparable();
  }
  
  //metodi "canonici"  
  public boolean IsEmpty() {
    return elem.IsEmpty();
  }
  
  public void Clear() {
    //svuota lista
    try {
      while(true)
        elem.DeleteHead();
    } catch (EmptyListException e) {}
  }

  public void Push(Object val) {
    elem.InsertHead(val);
  }
  
  public Object Pop() {
    if (IsEmpty())
      throw new StackEmptyException();
    return elem.DeleteHead();
  }
  
  public Object TopElem() {
    if (IsEmpty())
      throw new StackEmptyException();
    elem.rewind();
    return elem.getNextElem();
  }
    
}

//Eccezioni personalizzate per la classe Pila.
class StackEmptyException extends RuntimeException {
  public StackEmptyException() {
    super("Pila vuota!!");
  }
  public StackEmptyException(String msg) {
    super(msg);
  }
}

class StackFullException extends RuntimeException {
  public StackFullException() {
    super("Pila piena!!");
  }
  public StackFullException(String msg) {
    super(msg);
  }
}