Advertisement
Guest User

Untitled

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