Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. // codigo
  2.  
  3.   Scanner sc = new Scanner (System.in);
  4.        
  5.         System.out.println("Bienvenido");  
  6.         System.out.println("Digite el tamaño de la pila");
  7.        
  8.         int tamaño = sc.nextInt();
  9.         int dato = 0, opc = 0;
  10.        
  11.         Apilar a = new Apilar (tamaño);
  12.        
  13.         do {
  14.             System.out.println("Que desea hacer");
  15.             System.out.println("1. Ingresar dato ---- 2. Eliminar dato --- 3. Conocer si la pila esta vacia");
  16.             System.out.println("4. Tamaño de la pila --- 5. Salir");
  17.             opc = sc.nextInt();
  18.            
  19.            
  20.             switch (opc){
  21.                 case 1:
  22.                     System.out.println("Inserte los datos:");
  23.                     a.insertar(dato = sc.nextInt());
  24.                     System.out.println("el dato ha sido insertado");
  25.                     System.out.println("");
  26.                     break;
  27.                 case 2:
  28.                     a.eliminar();
  29.                     break;
  30.                 case 3:
  31.                     System.out.println(a.vacio());
  32.                     System.out.println("");
  33.                     break;
  34.                 case 4:
  35.                     System.out.println(a.tamaño());
  36.                     break;
  37.                    
  38.             }
  39.            
  40.         } while (opc !=5);
  41.          
  42.     }
  43.    
  44. }
  45.  
  46. // CLASE
  47.  int vectorpila[];
  48.     int cima;
  49.    
  50.     public Apilar  (int tamaño) {
  51.        
  52.       vectorpila= new int [tamaño];  
  53.        cima = 0;
  54.     }
  55.    
  56.     public void insertar (int dato) {
  57.         vectorpila [cima] = dato;
  58.         cima++;
  59.     }
  60.    
  61.    
  62.     public int eliminar (){
  63.         int eliminar =0;
  64.         if (cima == 0) {
  65.         System.out.println("no hay datos en la pila");
  66.     } else {
  67.             eliminar = vectorpila [cima];
  68.             cima--;
  69.         }
  70.         return eliminar;
  71. }
  72.    
  73.      public boolean vacio (){
  74.          if (cima == 0){
  75.              return true;
  76.          }else{
  77.              return false;
  78.          }
  79.          }
  80.      
  81.      public int tamaño (){
  82.          return vectorpila.length ;
  83.      }
  84.      
  85.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement