SHARE
TWEET

Untitled

a guest May 19th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package myintnodedouble;
  2.  
  3. public class MyIntNodeDouble {
  4.     private int num;
  5.     private MyIntNodeDouble next;
  6.     private MyIntNodeDouble prev;
  7.  
  8.     public MyIntNodeDouble(int num) {
  9.         this.num = num;
  10.         this.next = null;
  11.         this.prev = null;
  12.     }
  13.  
  14.     public int getNum() { return this.num; }
  15.     public void setNum(int n) { this.num = n; }
  16.  
  17.     public MyIntNodeDouble getNext() { return this.next; }
  18.     public void setNext(MyIntNodeDouble next) { this.next = next; }
  19.  
  20.     public MyIntNodeDouble getPrevious() { return this.prev; }
  21.     public void setPrev(MyIntNodeDouble previous) { this.prev = previous; }
  22.  
  23.     public void insertAfter(MyIntNodeDouble newNext) {
  24.         MyIntNodeDouble oldNext = this.next;
  25.         this.next = newNext;
  26.         newNext.prev = this;
  27.         newNext.next = oldNext;
  28.     }
  29.  
  30.     public void insertBefore(MyIntNodeDouble newPrev) {
  31.         MyIntNodeDouble oldPrev = this.prev;
  32.         this.prev = newPrev;
  33.         newPrev.prev = oldPrev;
  34.         newPrev.next = this;
  35.     }
  36.  
  37.     public void delete() {
  38.     this.next.prev = this.prev;
  39.     this.prev.next = this.next;
  40.     }
  41.  
  42.     public MyIntNodeDouble getFirst() {
  43.         MyIntNodeDouble current = this;
  44.         while (current.prev != null) {
  45.             current = current.prev;
  46.         }
  47.         return current;
  48.     }
  49.  
  50.     public MyIntNodeDouble getLast() {
  51.         MyIntNodeDouble current = this;
  52.         while (current.next != null) {
  53.             current = current.next;
  54.         }
  55.         return current;
  56.     }
  57.  
  58.     public void addFirst(int n) {
  59.         this.getFirst().prev = new MyIntNodeDouble(n);
  60.     }
  61.  
  62.     public void addLast(int n) {
  63.         this.getLast().next = new MyIntNodeDouble(n);
  64.     }
  65.  
  66.     public int size() {
  67.         MyIntNodeDouble current = this.getFirst();
  68.     if (current == null){
  69.             return 0;
  70.     }
  71.         int size = 1;
  72.         while (current.next != null) {
  73.             size++;
  74.             current = current.next;
  75.         }
  76.         return size;
  77.     }
  78.  
  79.     public int whereAmI() {
  80.         MyIntNodeDouble current = this.getFirst();
  81.         int pos = 0;
  82.         while (current != this) {
  83.             pos++;
  84.             current = current.next;
  85.         }
  86.         return pos;
  87.     }
  88.  
  89.     public void reverse() {
  90.         MyIntNodeDouble first = this.getFirst();
  91.         if (first == null) {
  92.               return;
  93.         }
  94.         MyIntNodeDouble lprev = first;
  95.         MyIntNodeDouble current = first.next;
  96.         first.next = null;
  97.         while (current != null) {
  98.             MyIntNodeDouble lnext = current.next;
  99.             current.next = lprev;
  100.             lprev = current;
  101.             current = lnext;
  102.         }
  103.         first = lprev;
  104.     }
  105.  
  106.     @Override public String toString() {
  107.         String returnString = "A list with " + this.size() + " integers:";
  108.         MyIntNodeDouble current = this.getFirst();
  109.         while (current != null) {
  110.           returnString += " " + current.num;
  111.           current = current.next;
  112.         }
  113.         return returnString;
  114.     }
  115.  
  116.     public static void main(String[] args) {
  117.         // Create Nodes
  118.         MyIntNodeDouble n1 = new MyIntNodeDouble(1);
  119.         MyIntNodeDouble n2 = new MyIntNodeDouble(2);
  120.         MyIntNodeDouble n3 = new MyIntNodeDouble(3);
  121.         MyIntNodeDouble n4 = new MyIntNodeDouble(4);
  122.         MyIntNodeDouble n5 = new MyIntNodeDouble(5);
  123.         MyIntNodeDouble n6 = new MyIntNodeDouble(6);
  124.         // Link Nodes Together
  125.         n1.setNext(n2); n2.setPrev(n1);
  126.         n2.setNext(n3); n3.setPrev(n2);
  127.         n3.setNext(n4); n4.setPrev(n3);
  128.         n4.setNext(n5); n5.setPrev(n4);
  129.         n5.setNext(n6); n6.setPrev(n5);
  130.         // Test methods
  131.         System.out.println(n1);
  132.         System.out.println(n4.whereAmI());
  133.         n4.delete();
  134.         System.out.println(n1);
  135.         n4.addLast(7);
  136.         System.out.println(n1);
  137.         n3.reverse();
  138.         System.out.println(n3);
  139.     }
  140.  
  141. }
  142.  
  143. /*
  144.  *run:
  145.  *A list with 5 integers: 1 2 3 4 5
  146.  *3
  147.  *A list with 4 integers: 1 2 3 5
  148.  *A list with 5 integers: 1 2 3 5 6
  149.  *A list with 0 integers:
  150.  *BUILD SUCCESSFUL (total time: 0 seconds)
  151. */
RAW Paste Data
Top