fefetl08

Pilha

Mar 18th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. public class Pilha {
  2.     private int topo;
  3.     private int[] dados;
  4.     private int max;
  5.  
  6.     public Pilha(int max) {
  7.         this.max = max;
  8.         this.topo = -1;
  9.         criar_pilha(this.max);
  10.     }
  11.     private void criar_pilha(int n) {
  12.         dados = new int[n];
  13.     }
  14.     public int topo() {
  15.         if (topo == -1) return -1;
  16.         else return dados[topo];
  17.     }
  18.     public boolean vazia() {
  19.         if (topo == -1) return true;
  20.         else return false;
  21.     }
  22.     private  boolean cheia() {
  23.         if (topo == dados.length - 1) return true;
  24.         else return false;
  25.     }
  26.     public void empilha(int elemento) {
  27.         if (!cheia()) {
  28.             topo = topo + 1;
  29.             dados[topo] = elemento;
  30.         }
  31.         else System.out.println("Pilha cheia");;
  32.     }
  33.     public Integer desempliha() {
  34.         if (!vazia()) {
  35.             int elemento = topo();
  36.             topo = topo - 1;
  37.             return elemento;
  38.         }
  39.         else {
  40.             System.out.println("Pilha vazia");
  41.             return null;
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment