StackTest.java 3.63 KB
Newer Older
Cesar Giulano Gonzalez Maqueda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
import java.util.Scanner;

class Stack {
    /*
    * Clase que representa un stack e implementa el push y el pop del stack. Acepta integers como floats
     */
    private int[] stack;
    private float[] stackf;
    private int capacidad;
    private int pointer;

    public float[] getStackf() {
        return stackf;
    }

    public void setStackf(float[] stackf) {
        this.stackf = stackf;
    }

    public int getPointer() {
        return pointer;
    }

    public void setPointer(int pointer) {
        this.pointer = pointer;
    }

    public int getCapacidad() {
        return capacidad;
    }

    public void setCapacidad(int capacidad) {
        this.capacidad = capacidad;
    }

    public Stack(int capacidad, int[] stack, float[] stackf){
        this.capacidad = capacidad;
        this.stack = stack;
        this.pointer = capacidad - 1;
        this.stackf = stackf;
    }

    public void push(float number){
        if(pointer + 1 == capacidad){
            System.out.println("El stack esta a maxima capacidad");
        }else{
            this.stack[pointer+1] = (int)number;
            this.stackf[pointer+1] = number;
            pointer++;
        }
    }

    public void push(int number){
        if(pointer + 1 == capacidad){
            System.out.println("El stack esta a maxima capacidad");
        }else{
            this.stack[pointer+1] = number;
            this.stackf[pointer+1] = (float)number;
            pointer++;
        }
    }


    public int pop(){
        if(pointer - 1 < -1){
            System.out.println("Ya no existen datos en el stack");
        }else{
            pointer--;
            return this.stack[pointer+1];
        }
        return -1;
    }

    public int[] getStack() {
        return stack;
    }

    public void setStack(int[] stack) {
        this.stack = stack;
    }

    public void printStack(){
        for(int i=pointer;i>=0;i--){
            System.out.println(stackf[i]);
        }
    }
}


public class StackTest{

    public static void main(String[] args){
        /*
        * Menu de gestion del stack.
        * */
        int choice = 0;
        int cantidad;
        int dato;
        boolean continuar = true;
        Scanner in = new Scanner(System.in);
        System.out.println("Ingrese la cantidad de datos de su stack: ");
        cantidad = in.nextInt();
        int[] datos = new int[cantidad];
        float[] datosf = new float[cantidad];
        for(int i=0;i<cantidad;i++){
            datos[i] = (int)(Math.random()*100);
            datosf[i] = (float)datos[i];
        }
        Stack stack = new Stack(cantidad, datos, datosf);
        do{
            System.out.println("-------------------\n1 - push()\n2 - pop()\n3 - Imprimir Stack\n4 - Salir\nEntrada: ");
            try{
                choice = in.nextInt();
            }catch (Exception e){
                System.out.println("Entrada no validad");
            }
            switch (choice){
                case 1:
                    System.out.println("Ingrese un numero: ");
                    dato = in.nextInt();
                    stack.push(dato);
                    break;
                case 2:
                    System.out.println("Pop de : "+stack.pop());
                    break;
                case 3:
                    System.out.println("Stack Actual: ");
                    stack.printStack();
                    break;
                case 4:
                    System.out.println("Saliendo");
                    continuar = false;
                    break;
                default:
                    System.out.println("Eleccion no validad");
            }
        }while(continuar);
    }

}