class Pila{
private class NodoPila{
public int elemento;
public NodoPila resto;
public NodoPila(int e, NodoPila r){
elemento = e;
resto = r;
}
}
private NodoPila top;
private Pila(NodoPila np){
top = np;
}
public static vacia(){
return new Pila(null);
}
public boolean esVacia(){
return top == null;
}
public void apilar(int e){
top = new NodoPila(e, top);
}
public int desapilar() throws PilaVaciaException{
if (top == null) throw new PilaVaciaException();
int res = top.elemento;
top = top.resto;
return res;
}
}
class PilaVaciaException extends Exception{}