Advertisement
Guest User

ExercicioPilha

a guest
Mar 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package estrudadospilha;
  7.  
  8. import javax.swing.JOptionPane;
  9.  
  10. /**
  11. *
  12. * @author Lucas
  13. */
  14. public class EstruDadosPILHA {
  15.  
  16. private int vetor[] = new int[10];
  17. private int quantidade;
  18. private int menu;
  19. private int numero = 0;
  20.  
  21. public static void main(String[] args) {
  22. EstruDadosPILHA rodar = new EstruDadosPILHA();
  23. rodar.menu();
  24. }
  25.  
  26. public void menu() {
  27. do {
  28. menu = Integer.parseInt(JOptionPane.showInputDialog("Menu"
  29. + "\n"
  30. + "\n1- Inserir elemento na pilha"
  31. + "\n2- Remover elemento da pilha"
  32. + "\n3- Quantidade de elementos na pilha"
  33. + "\n4- Mostrar o elemento de cima da pilha"
  34. + "\n5- Sair"));
  35.  
  36. switch (menu) {
  37. case 1:
  38. numero = Integer.parseInt(JOptionPane.showInputDialog("Número que você vai adicionar a pilha"));
  39. inserirInicio();
  40. mostrar();
  41. break;
  42.  
  43. case 2:
  44. removerInicio();
  45. mostrar();
  46. break;
  47.  
  48. case 3:
  49. quantidade();
  50. break;
  51.  
  52. case 4:
  53. mostrarElementoFinal();
  54. break;
  55.  
  56. }
  57. } while (menu < 5);
  58.  
  59. }
  60.  
  61. public void inserirInicio() {
  62. if (quantidade < 10) {
  63. for (int i = quantidade; i > 0; i--) {
  64. vetor[i] = vetor[i - 1];
  65. }
  66. vetor[0] = numero;
  67. quantidade++;
  68. System.out.println("Elemento inserido");
  69. } else {
  70. System.out.println("Pilha completa");
  71. }
  72.  
  73. }
  74.  
  75. public void removerInicio() {
  76. if (this.quantidade < 10) {
  77. this.vetor[0] = 0;
  78. }
  79. for (int i = 0; i < quantidade; i++) {
  80. vetor[i] = vetor[i + 1];
  81. }
  82. quantidade++;
  83. System.out.println("Elemento removido");
  84. }
  85.  
  86. public void quantidade() {
  87. System.out.println("A quantidade de elementos da pilha é de: " + quantidade);
  88.  
  89. }
  90.  
  91. public void mostrarElementoFinal() {
  92. if (quantidade > 0) {
  93. System.out.println("O elemento de cima da pilha é: " + vetor[quantidade - 1]);
  94.  
  95. }
  96.  
  97. }
  98.  
  99. public void mostrar() {
  100. for (int i = 0; i < vetor.length; i++) {
  101. System.out.println("\n[" + vetor[i] + "]");
  102. }
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement