lunes, 24 de octubre de 2016

PILAS 
son  una estructura de datos que tienendos operaciones:
push (para insertar un elemento) y pop (para extraer un elemento). su caracteristica importante es que al extraer se optiene siempre el ultimo elemento.
Ejemplo
codigo:

public class pila {
    private Object []repPila;
    private int cabPila;
    private int maxPila;
    public pila ()
    {this.cabPila=0;
     this.maxPila=10;
     this.repPila= new Object[this.maxPila];    
    }
     public pila (int mP)
    {this.cabPila=0;
     this.maxPila=mP;
     this.repPila= new Object[this.maxPila];    
    }
      public boolean EstaLLena ()
     {
        boolean estado=false ;
        if(this.cabPila>=this.maxPila)
            estado= true;
        return estado;
     }
     public boolean EstaVacia ()
     {
        boolean estado=false ;
        if(this.cabPila==0)
            estado= true;
        return estado;
     }
     
     public boolean Apilar(Object e)
     { boolean apilo=false;
      if(!this.EstaLLena())
        {
           this.repPila[this.cabPila]=new Object();
           this.repPila[this.cabPila++]=e;
           apilo =true;
        }
        return apilo;  
     }
     public Object Desapilar()
     {
         Object tempElem=null;
         if(!this.EstaVacia())
             tempElem=this.repPila[--this.cabPila];
       return tempElem;       
     }

    public Object[] getRepPila() {
        return repPila;
    }

    public int getCabPila() {
        return cabPila;
    }

    public void setRepPila(Object[] repPila) {
        this.repPila = repPila;
    }

    public void setCabPila(int cabPila) {
        this.cabPila = cabPila;
    }
     
}

No hay comentarios:

Publicar un comentario