Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. import java.util.AbstractList;
  2.  
  3. public class MyLinkedList<E extends Comparable<E>> extends AbstractList<E>
  4. {
  5.   private Node first, last;
  6.   private int size;
  7.  
  8.   private void printInReverse(Node ref)
  9.   {
  10.     if (ref != null)
  11.     {
  12.       printInReverse(ref.next);
  13.       System.out.println(ref.value);
  14.     }
  15.   }
  16.  
  17.   void printInReverse()
  18.   {
  19.     printInReverse(first);
  20.   }
  21.  
  22.   public E remove(int index)
  23.   {
  24.     if (index < 0 || index >= size())
  25.     {
  26.       throw new IndexOutOfBoundsException("Invalid index: " + index);
  27.     }
  28.  
  29.     if (index == 0)
  30.     {
  31.       first = first.next; //garbage collect the old "first"
  32.       if (first == null)
  33.       {
  34.         last = null; //now list is empty
  35.       }
  36.     } else {
  37.       Node ref = first;
  38.       //travel to the node right before "index"
  39.       for (int x = 0; x < index - 1; x++)
  40.       {
  41.         ref = ref.next;
  42.       }
  43.       ref.next = ref.next.next;
  44.       if (ref.next == null)
  45.       {
  46.         last = ref;
  47.       }
  48.     }
  49.     size--;
  50.     return null;
  51.   }
  52.  
  53.   //O(N)
  54.   public void add(int index, String value)
  55.   {
  56.     if (index < 0 || index > size())
  57.     {
  58.       throw new IndexOutOfBoundsException("Invalid index: " + index);
  59.     }
  60.     if (index == 0)
  61.     {
  62.       first = new Node(value, first);
  63.     } else {
  64.       Node ref = first;
  65.       //travel to the node right before "index"
  66.       for (int x = 0; x < index - 1; x++)
  67.       {
  68.         ref = ref.next;
  69.       }
  70.       //make the connection
  71.       ref.next = new Node(value, ref.next);
  72.     }
  73.     if (last.next != null)
  74.     {
  75.       last = last.next;
  76.     }
  77.     size++;
  78.   }
  79.  
  80.   public String toString()
  81.   {
  82.     //way to concatenate many Strings together
  83.     StringBuilder result = new StringBuilder();
  84.     Node ref = first;
  85.     while (ref != null)
  86.     {
  87.       result.append(ref.value + "\n");
  88.       ref = ref.next;
  89.     }
  90.     return result.toString();
  91.   }
  92.  
  93.   @SuppressWarnings("unchecked")
  94.   public E get (int index)
  95.   {
  96.     if (index < 0 || index >= size())
  97.     {
  98.       throw new IndexOutOfBoundsException("Invalid index: " + index);
  99.     }
  100.  
  101.     //starting at very first node
  102.     Node ref = first;
  103.     for (int x = 0; x < index; x++)
  104.     {
  105.       // x does not = subscript
  106.       ref = ref.next; //hop to next node
  107.     }
  108.     return (E) ref.value;
  109.   }
  110.  
  111.   void add(String value)
  112.   {
  113.     //check if it's empty 1st
  114.     if (isEmpty())
  115.     {
  116.       first = last = new Node(value);
  117.     }
  118.     else
  119.     {
  120.       last.next = new Node(value);
  121.       last = last.next; //leap frog!
  122.     }
  123.     size++;
  124.   }
  125.  
  126.   //method to check if linked list is empty - first node is empty
  127.   public boolean isEmpty()
  128.   {
  129.     return first == null;
  130.   }
  131.  
  132.   public int size()
  133.   {
  134.     return size; //O(1) - constant time
  135.   }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement