Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. public class LList<T>
  2. {
  3.     private Node<T> firstNode;
  4.     private Node<T> lastNode;
  5.     private int length;
  6.  
  7.     public LList()
  8.     {
  9.         this.clear();
  10.     }
  11.  
  12.     public final void clear()
  13.     {
  14.         this.firstNode = null;
  15.         this.lastNode = null;
  16.         this.length = 0;
  17.     }
  18.  
  19.     public int size()
  20.     {
  21.         return  this.length;
  22.     }
  23.  
  24.     public boolean isEmpty()
  25.     {
  26.         return  this.length == 0;
  27.     }
  28.  
  29.     public String toString()
  30.     {
  31.  
  32.         if (this.length==0) return "[]";  //exit!
  33.  
  34.         String r =  "[";
  35.  
  36.         Node<T> curr = this.firstNode;
  37.  
  38.         for (int i=0; i<=this.length-2; i++)
  39.         {
  40.             r = r + curr.data.toString() + ", ";
  41.             curr = curr.next;
  42.         }
  43.  
  44.         r = r + curr.data.toString() + "]";
  45.  
  46.         return r;
  47.     }
  48.  
  49.     public void display()
  50.     {
  51.         Node<T> curr = this.firstNode;
  52.         while (curr != null)
  53.         {
  54.             System.out.println("*" + curr.data);
  55.             curr = curr.next;
  56.         }
  57.     }
  58.  
  59.     private Node<T> getNodeAt(int position)   ///////a private "helper" function
  60.     {
  61.  
  62.         //preconditions: list is not empty.  1 <= position <= this.length
  63.  
  64.         Node<T> curr = this.firstNode;
  65.         for (int i=1; i<position; i++)
  66.         {
  67.             curr = curr.next;
  68.         }
  69.         return curr;
  70.     }
  71.  
  72.     public T get(int idx)
  73.     {
  74.         if (idx < 1  ||  idx > this.length)
  75.         {
  76.             return null;
  77.         }
  78.         else
  79.         {
  80.             T result = this.getNodeAt(idx).data;
  81.             return result;
  82.  
  83.             //can combine these 2 lines into 1 line, like this:
  84.             //return  this.getNodeAt(idx).data;
  85.         }
  86.     }
  87.  
  88.     public void set(int idx, T a)
  89.     {
  90.         if (idx < 1 || idx > this.length)
  91.         {
  92.             System.out.println("Illegal index, ignoring");
  93.         }
  94.         else
  95.         {
  96.             Node<T> desiredNode = this.getNodeAt(idx);
  97.             desiredNode.data = a;
  98.             //can combine these 2 lines into 1 line
  99.         }
  100.     }
  101.  
  102.     public boolean contains(T a)
  103.     {
  104.         Node<T> curr = this.firstNode;
  105.         while (curr != null)
  106.         {
  107.             if (a.equals(curr.data)) {return true;}  //exit!
  108.             curr = curr.next;
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     public void add(T a)   //add to back
  114.     {
  115.         Node<T> newlast = new Node<T>(a);
  116.  
  117.         if (this.isEmpty())
  118.         { this.firstNode = newlast; }
  119.         else
  120.         { this.lastNode.next = newlast; }
  121.  
  122.         this.lastNode = newlast;
  123.         this.length++;
  124.     }
  125.  
  126.     public void add(int idx, T a)   //insert
  127.     {
  128.  
  129.         if (idx < 1  ||  idx > this.length+1)
  130.         {
  131.             System.out.println("Illegal index, ignoring");
  132.             return;  //exit!
  133.         }
  134.  
  135.         Node<T> nn = new Node<T>(a);
  136.         if (this.isEmpty())
  137.         {
  138.             this.firstNode = nn;
  139.             this.lastNode = nn;
  140.         }
  141.         else if (idx == 1)
  142.         {
  143.             nn.next = this.firstNode;
  144.             this.firstNode = nn;
  145.         }
  146.         else if (idx == this.length+1)
  147.         {
  148.             this.lastNode.next = nn;
  149.             this.lastNode = nn;
  150.         }
  151.         else
  152.         {
  153.             Node<T> nodeBefore = this.getNodeAt(idx-1);
  154.             Node<T> nodeAfter = nodeBefore.next;
  155.             nn.next = nodeAfter;
  156.             nodeBefore.next = nn;
  157.         }
  158.  
  159.         this.length++;
  160.  
  161.     } //add, insert
  162.  
  163.     public T remove(int idx)
  164.     {
  165.  
  166.         if (idx < 1  ||  idx > this.length)
  167.         {
  168.             return null;  //exit!
  169.         }
  170.  
  171.         T result;
  172.         if (idx == 1)
  173.         {
  174.             result = this.firstNode.data;
  175.             this.firstNode = this.firstNode.next;
  176.             if (this.length==1) this.lastNode=null;
  177.         }
  178.         else
  179.         {
  180.             Node<T> nodeBefore = this.getNodeAt(idx-1);
  181.             Node<T> nodeToRemove = nodeBefore.next;
  182.             Node<T> nodeAfter = nodeToRemove.next;
  183.             result = nodeToRemove.data;
  184.             nodeBefore.next = nodeAfter;
  185.             if (idx==this.length) this.lastNode=nodeBefore;
  186.         }
  187.         this.length--;
  188.         return result;
  189.     }
  190.    
  191.     public void insertAtSorted(T x)
  192.     {
  193.         Node<T> nn = new Node<T>(x);
  194.         T check = this.getNodeAt(this.length/2).data;
  195.         if (check.equals(x))
  196.         {
  197.             this.add(this.length/2, x);
  198.         }
  199.         else if(check.compareTo(x) < -1)
  200.         {
  201.         }
  202.         else
  203.         {
  204.            
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement