cesarnascimento

node

Mar 2nd, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. package estdados;
  2.  
  3. public class List2 {
  4.  
  5.     static Node first = null;
  6.     static Node last = null;
  7.  
  8.     public static void add(int value) {
  9.        
  10.         Node a = new Node();
  11.        
  12.         a.setInfo(value);
  13.        
  14.        
  15.         if(first == null) {
  16.             first = a;
  17.             last = a;
  18.         }else {
  19.             last.setNext(a);
  20.             last = a;
  21.         }
  22.     }
  23.    
  24.     public void delete() {
  25.         if(first.getNext() != null) {
  26.             Node penultimate = retrieveNextElement(first.getNext());
  27.             penultimate.setNext(null);
  28.             last = penultimate;
  29.         }else {
  30.             first = last = null;
  31.         }
  32.    
  33.     }
  34.    
  35.     private Node retrieveNextElement(Node element) {
  36.         if(element.getNext().equals(last)) {
  37.             return element;
  38.         }
  39.        
  40.         return retrieveNextElement(element.getNext());
  41.     }
  42. }
  43.  
  44.  
  45. //node
  46.  
  47. package estdados;
  48.  
  49. public class Node {
  50.  
  51.     private int info;
  52.     private Node next;
  53.  
  54.     public int getInfo() {
  55.         return info;
  56.     }
  57.  
  58.     public void setInfo(int info) {
  59.         this.info = info;
  60.     }
  61.  
  62.     public Node getNext() {
  63.         return next;
  64.     }
  65.  
  66.     public void setNext(Node next) {
  67.         this.next = next;
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment