Advertisement
cesarnascimento

pilha

Mar 23rd, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. package pilha;
  2.  
  3. public class Pilha {
  4.  
  5.     int[] elements;
  6.     int top = 0;
  7.     int remove = 0;
  8.  
  9.     public Pilha() {
  10.         elements = new int[2];
  11.     }
  12.  
  13.     public void push(int value) {
  14.         if (!isFull()) {
  15.             elements[top] = value;
  16.             top++;
  17.         } else {
  18.             System.out.println("Quebrou");
  19.         }
  20.     }
  21.  
  22.     public int pop() {
  23.         if (!isEmpty()) {
  24.             remove = elements[--top];
  25.             elements[top] = 0;
  26.             return remove;
  27.         } else {
  28.             return -1;
  29.         }
  30.     }
  31.  
  32.     private boolean isFull() {
  33.         if (top == elements.length)
  34.             return true;
  35.  
  36.         return false;
  37.     }
  38.  
  39.     private boolean isEmpty() {
  40.         if (top == 0)
  41.             return true;
  42.  
  43.         return false;
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement