//PROGRAMA5. PILA. Son dos archivos "Pila.java" que define la interface de pila y //"PilaLista.java"que implementa la pila
//////////////////////////////////////...
//archivo Pila.java
package carlosFontela.utilidades
public interface Pila {
void agregar (Object o );
Object extraer();
Object verTope();
boolean estavacia();
void Vaciar()
//////////////////////////////////////...
//archivo PilaLista.java
package carlosFontela.utilidades
public class PilaLista implements Pila{
private Object elementoTope;
private PilaLista restoLista;
//constuctor
public PilaLista(){
elementoTope = null;
restoLista = null;
}
// SETER
public void agregar (Object o){
PilaLista pilaNueva = newPilaLista();
pilaNueva.elementoTope = this.elementoTope;
pilaNueva.restoLista = this.restoLista;
restoLista = pilaNueva;
elementoTope = o;
}
//GETER
public Object extraer(){
if(estaVacia())
throw new java.util.NoSuchElementException();
Object tope = elementoTope;
if (restoLista == null)
elementoTope = null;
else {
elementoTope = restoLista.elementoTope;
restoLista = restoLista.restoLista;
}
return tope;
}
//recuperar el primer elemento
public Object verTope(){
if (estaVacia())
throw new java.util.NoSuchElementException();
return elementoTope;
}
//revisar estado
public boolean estaVacia(){
return (elementoTope == null);
}
//destructor
public void Vaciar(){
elementoTope = null;
}
}//end class
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario