Stack1.java 3.74 KB
Newer Older
Javier Ferreira 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stack;

import java.util.Scanner;

class Stack {
    
    
    
    private int[] stack;
    private float[] stackjf;
    private int cap;
    private int puntero;

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

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

    public int getPointer() {
        return puntero;
    }

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

    public int getCapacidad() {
        return cap;
    }

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

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

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

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


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

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

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

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

/**
 *
 * @author user
 */
public class Stack1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     int choice = 0;
        int cantidad;
        int dato;
        boolean continuar = true;
        Scanner in = new Scanner(System.in);
        System.out.println("Rellene la pila: ");
        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("Usuario 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("Pila actual: ");
                    stack.printStack();
                    break;
                case 4:
                    System.out.println("Salir");
                    continuar = false;
                    break;
                default:
                    System.out.println("Eleccion no valida");
            }
        }while(continuar);

        

// TODO code application logic here
    }
    
}