cesarnascimento

pilha node dinamica

Mar 23rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. package pilha;
  2.  
  3. public class Node {
  4.  
  5.     private Node next = null;
  6.     private int value;
  7.  
  8.     public Node getNext() {
  9.         return next;
  10.     }
  11.  
  12.     public void setNext(Node next) {
  13.         this.next = next;
  14.     }
  15.  
  16.     public int getValue() {
  17.         return value;
  18.     }
  19.  
  20.     public void setValue(int value) {
  21.         this.value = value;
  22.     }
  23. }
  24.  
  25. package pilha;
  26.  
  27. public class PilhaDin {
  28.  
  29.     Node top = null;
  30.  
  31.     public void push(Node element) {
  32.         if (isEmpty()) {
  33.             top = element;
  34.         }else {
  35.             element.setNext(top);
  36.             top = element;
  37.         }
  38.     }
  39.  
  40.     public void pop() {
  41.         if (!isEmpty()) {
  42.             top = top.getNext();       
  43.         }else {
  44.             System.out.println("Vazia");
  45.         }
  46.     }
  47.  
  48.     private boolean isEmpty() {
  49.         if (top == null)
  50.             return true;
  51.  
  52.         return false;
  53.     }
  54. }
Add Comment
Please, Sign In to add comment