Stack.java 2.25 KB
Newer Older
willgonzz 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
import java.util.stream.IntStream;

public class Stack{
    public int arrays[];
    public int dimension;
   // public float[]arraysF;
    public int frente;
    //public int frenteF;
    public void Stack(){
        this.arrays=new int[5];
        dimension=5;
        //this.arraysF=new float[5];
    }
    public Stack(int dimension){
        this.arrays=new int[dimension];
        //this.arraysF=new float[dimension];
        this.dimension=dimension;
        frente=0;
        //frenteF=0;
    }
    public void dimension(){
        System.out.println(arrays.length);
    }
    public void push(int valor){ 
        if(frente<dimension){
            this.arrays[frente]=valor;
            frente++;
        }else{System.out.println("La Pila llena");}
    }
    
    /*public void push(float dato){
        if(frenteF<dimension){ 
            arraysF[frenteF]=dato;
            frenteF++;
        }else{System.out.println("La Pila llena");}
    }*/

    public int pop(){
        if(frente>0){
            frente--;
            return arrays[frente];
        }else{return 0;}

    }
    /*public float popf(){
        if(frente>0){
            frenteF--;
            return arraysF[frenteF];
        }else{return 0;}
    }*/
    public static void ordenarPila(Stack a){
        if(a.frente!=0){
            int temp= a.pop();
            ordenarPila(a);
            insertarOrden(a,temp);
        }
    }
    public static void insertarOrden(Stack a, int temp){
        if(a.frente==0 || temp>a.arrays[a.frente-1]){
            a.push(temp);
        }else{
            int dato=a.pop();
            insertarOrden(a, temp);
            a.push(dato);
        }
    }

    public static void main(String[] args) {
        Stack a= new Stack(10);
        //a.dimension();
        a.push(5);
        a.push(4);
        a.push(7);
        a.push(6);
        a.push(3);
        a.push(1);
        a.push(2);
        ordenarPila(a);
        //a.push(5.5f);
        System.out.println(a.pop());
        System.out.println(a.pop());
        System.out.println(a.pop());
        System.out.println(a.pop());
        System.out.println(a.pop());
        System.out.println(a.pop());
        System.out.println(a.pop());
        //System.out.println(a.pop());
        //System.out.println(a.popf());
    }
   
}