miércoles, 19 de septiembre de 2012

PILA IMPLEMENTADO CON ARRAY EN JAVA

Este es un ejemplo de la implementación de una PILA en JAVA utilizando un ARRAY.

EJERCICIO PILA:
  • RESULTADO




  • CODIGO FUENTE PILA

/**
 *
 *  E-Mail : shamirdhc31@gmail.com
 *  Blog   : http://javadhc.blogspot.com
 *
 */

import javax.swing.JOptionPane;

public class PilaArray 
{
    int[] PILA;
    int TOPE;
    int MAX;

    //-- CUANDO NO SE LE ASIGNA EL TAMAÑO MAXIMO
    public PilaArray()
    {
        this.TOPE = 0;
        this.MAX = 100;
        this.PILA = new int[this.MAX+1];
    }
    
    //-- CUANDO SE LE ASIGNA EL TAMAÑO MAXIMO
    public PilaArray(int MAX)
    {
        this.TOPE = 0;
        this.MAX = MAX;
        this.PILA = new int[this.MAX+1];        
    }

    public int GetTOPE() 
    {
        return TOPE;
    }
      
    public void VaciarPila()
    {
        this.TOPE = 0;         
    }
    
    public boolean IsPilaLlena()
    {
        if(this.MAX == this.TOPE)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public boolean IsPilaVacia()
    {
        if(this.TOPE == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void InsertarPila()
    {
        if(this.IsPilaLlena())
        {
            JOptionPane.showMessageDialog(null,"LA PILA ESTA LLENA");
        }
        else
        {
            int ITEM;

            ITEM = Integer.parseInt(JOptionPane.showInputDialog(null,"INGRESE EL ITEM A AGREGAR"));            
            
            this.TOPE++;
            this.PILA[this.TOPE] = ITEM;
            
            JOptionPane.showMessageDialog(null,"EL ITEM " + ITEM + " SE INSERTO A LA PILA");
        }        
    }
    
    public void EliminarPila()
    {
        if(this.IsPilaVacia())
        {
            JOptionPane.showMessageDialog(null,"LA PILA ESTA VACIA");
        }
        else
        {
            int ITEM = this.PILA[this.TOPE];
            this.TOPE--;
            
            JOptionPane.showMessageDialog(null,"EL ITEM " + ITEM + " SE ELIMINO DE LA PILA");
        }        
    }
    
    public void MostrarPila()
    {
        if(this.IsPilaVacia())
        {
            JOptionPane.showMessageDialog(null,"LA PILA ESTÁ VACIA\n NO HAY DATOS QUE MOSTRAR","MOSTRAR DATOS",JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            int i;
            String MOSTRAR = "";
            
            for(i=1;i<=this.TOPE;i++)
            {
                MOSTRAR = MOSTRAR + this.PILA[i]+"\n";
            }
            JOptionPane.showMessageDialog(null,"TOTAL ES : "+this.TOPE+"\n"+"LOS DATOS DE LA PILA SON : \n"+MOSTRAR,"MOSTRAR DATOS",JOptionPane.INFORMATION_MESSAGE);
        }
    }    
}

  • CODIGO FUENTE MAIN PILA

/**
 *
 *  E-Mail : shamirdhc31@gmail.com
 *  Blog   : http://javadhc.blogspot.com
 *
 */

import javax.swing.JOptionPane;

public class MainPilaArray 
{
    public static PilaArray Pila;
    
    public static void main(String[] ARGUMENTOS)
    {    
        int MAX;

        MAX = Integer.parseInt(JOptionPane.showInputDialog(null,"INGRESE EL TAMAÑO DE LA PILA"));   
        Pila = new PilaArray(MAX);
        
        MenuPila();
    }
    
    public static void MenuPila()
    {
        int Opcion;
        
        do
        {
            Opcion = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "1. INGRESAR DATOS\n"+
                    "2. ELIMINAR DATOS\n"+
                    "3. OBSERVAR DATOS\n"+
                    "4. VACIAR PILA\n"+
                    "5. SALIR\n"+
                    "--------------------------------------------------------\n"+
                    "INGRESE LA OPCION [1 - 5]","MENU PILA",JOptionPane.QUESTION_MESSAGE));
            
            switch(Opcion)
            {
                case 1: Pila.InsertarPila();break;
                case 2: Pila.EliminarPila();break;
                case 3: Pila.MostrarPila();break;
                case 4: Pila.VaciarPila();break;
                case 5: System.exit(0);break;
                default: JOptionPane.showMessageDialog(null,"INGRESE UNA OPCION VALIDA \n","ERROR OPCION",JOptionPane.WARNING_MESSAGE);break;
            }
        }
        while(true); //-- SEGUIRA HASTA QUE OPCION SEA IGUAL A 5
    }
}

DALE CLICK EN LA IMAGEN PARA BAJARTE EL PROYECTO CON EL EJERCICIO


IMPORTANTE : "TODOS LOS CODIGOS INDICADOS AQUI SON ESCRITOS POR MI PERSONA, ASI QUE CUALQUIER DUDA O EJERCICIO QUE NO PUEDAN RESOLVER, NO DUDEN EN MANDARME UN E-MAIL A MI CORREO"
shamirdhc31@gmail.com

3 comentarios: