3-2do PA CODIGO

 

Pilas y Colas Con Arrays


Pilas-Arrays

Clase Pila


package pilas;

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);

        }

    }    

}


Clase Main

package pilas;


import javax.swing.JOptionPane;


public class MainPilaArray extends PilaArray

{

    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. Mostrar 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

    }

}



Cola

Clase Cola













package colas;

import javax.swing.JOptionPane;

public class ColaArray {
    private int MAX;
    private int[] COLA;
    private int FRENTE;
    private int FINAL;

    public ColaArray()
    {
        this.MAX = 100;
        this.FRENTE = 0;
        this.FINAL = 0;
        this.COLA = new int[this.MAX+1];
    }

    public ColaArray(int MAX)
    {
        this.MAX = MAX;
        this.FRENTE = 0;
        this.FINAL = 0;  
        this.COLA = new int[this.MAX+1];
    }  
    
    public boolean IsColaLlena()
    {
        if(this.FINAL == this.MAX)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean IsColaVacia()
    {
        if(this.FRENTE == this.FINAL)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void InsertarCola()
    {
        if(this.IsColaLlena())
        {
            JOptionPane.showMessageDialog(null,"LA COLA ESTÁ LLENA");
        }
        else
        {
            int ITEM;

            ITEM = Integer.parseInt(JOptionPane.showInputDialog(null,"INGRESE EL ITEM A AGREGAR"));  
            
            this.FINAL++; 
            this.COLA[FINAL] = ITEM;
                       
        }
    }

    public void EliminarCola()
    {
        if(this.FRENTE == this.FINAL)
        {
            JOptionPane.showMessageDialog(null,"LA COLA ESTA VACIA");
        }
        else
        {            
            for(int i=FRENTE;i < FINAL - 1;i++)
            {
                COLA[i] = COLA[i+1];
            }
            
            FINAL--;
        }
    }

    public void MostrarCola()
    {
        if(this.IsColaVacia())
        {
            JOptionPane.showMessageDialog(null,"LA COLA ESTÁ VACIA\n NO HAY DATOS QUE MOSTRAR","MOSTRAR DATOS",JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            String MOSTRAR = "";
            
            for(int i=FRENTE+1;i<=FINAL;i++)
            {
                MOSTRAR = MOSTRAR + COLA[i]+"\n";
            }
            
            JOptionPane.showMessageDialog(null,"TOTAL ES : "+this.FINAL+"\n"+"LOS DATOS DE LA COLA SON : \n"+MOSTRAR,"MOSTRAR DATOS",JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public void VaciarCola()
    {
        FRENTE = 0;
        FINAL = 0;
    }       
}

Clase Main

package colas;

import javax.swing.JOptionPane;

public class MainColaArray extends ColaArray
{
    public static ColaArray Cola;
    
    public static void main(String[] ARGUMENTOS)
    {    
        int MAX;

        MAX = Integer.parseInt(JOptionPane.showInputDialog(null,"INGRESE EL TAMAÑO DE LA COLA"));   
        Cola = new ColaArray(MAX);
        
        MenuCola();
    }
    
    public static void MenuCola()
    {
        int Opcion;
        
        do
        {
            Opcion = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "1. Ingresar Datos\n"+
                    "2. Elimunar Datos\n"+
                    "3. Mostrar Datos\n"+
                    "4. Vaciar Cola\n"+
                    "5. Salir\n"+
                    "--------------------------------------------------------\n"+
                    "INGRESE LA OPCION [1 - 5]","MENU COLA",JOptionPane.QUESTION_MESSAGE));
            
            switch(Opcion)
            {
                case 1: Cola.InsertarCola();break;
                case 2: Cola.EliminarCola();break;
                case 3: Cola.MostrarCola();break;
                case 4: Cola.VaciarCola();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
    }

}

Comentarios

Entradas más populares de este blog

6-3er PA