Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 estrudadospilha;
- import javax.swing.JOptionPane;
- /**
- *
- * @author Lucas
- */
- public class EstruDadosPILHA {
- private int vetor[] = new int[10];
- private int quantidade;
- private int menu;
- private int numero = 0;
- public static void main(String[] args) {
- EstruDadosPILHA rodar = new EstruDadosPILHA();
- rodar.menu();
- }
- public void menu() {
- do {
- menu = Integer.parseInt(JOptionPane.showInputDialog("Menu"
- + "\n"
- + "\n1- Inserir elemento na pilha"
- + "\n2- Remover elemento da pilha"
- + "\n3- Quantidade de elementos na pilha"
- + "\n4- Mostrar o elemento de cima da pilha"
- + "\n5- Sair"));
- switch (menu) {
- case 1:
- numero = Integer.parseInt(JOptionPane.showInputDialog("Número que você vai adicionar a pilha"));
- inserirInicio();
- mostrar();
- break;
- case 2:
- removerInicio();
- mostrar();
- break;
- case 3:
- quantidade();
- break;
- case 4:
- mostrarElementoFinal();
- break;
- }
- } while (menu < 5);
- }
- public void inserirInicio() {
- if (quantidade < 10) {
- for (int i = quantidade; i > 0; i--) {
- vetor[i] = vetor[i - 1];
- }
- vetor[0] = numero;
- quantidade++;
- System.out.println("Elemento inserido");
- } else {
- System.out.println("Pilha completa");
- }
- }
- public void removerInicio() {
- if (this.quantidade < 10) {
- this.vetor[0] = 0;
- }
- for (int i = 0; i < quantidade; i++) {
- vetor[i] = vetor[i + 1];
- }
- quantidade++;
- System.out.println("Elemento removido");
- }
- public void quantidade() {
- System.out.println("A quantidade de elementos da pilha é de: " + quantidade);
- }
- public void mostrarElementoFinal() {
- if (quantidade > 0) {
- System.out.println("O elemento de cima da pilha é: " + vetor[quantidade - 1]);
- }
- }
- public void mostrar() {
- for (int i = 0; i < vetor.length; i++) {
- System.out.println("\n[" + vetor[i] + "]");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement