title: Sorted linked list implementation
body: <p>i am a novice programmer, to be specific, i am learning java programming and i am supposed to implement sortedLinkedList class that extends LinkedList class from the java library. The list has to store persons in ascending order of their surnames. I have already written my Person class that implements Comparable interface. my problem is, I have been struggling implementing this sortedLinkedClass but to no avail. My code runs without any compiling or run time error but the program does not print anything. Another thing as you can see , I am testing it with Integers instead of Persons and it throws NullPointerException when trying to add a number that is already in the list. My code is as it is below.</p>
  
  <pre><code>import java.util.*;
  public class SortedLinkedList&lt; E&gt; extends LinkedList&lt;E&gt; 
  {
     private Link&lt;E&gt; first;
     private Link&lt;E&gt; last;
  
  /**
   * Constructor for objects of class SortedLinkedList
   */
  public SortedLinkedList()
  {
      //super();
      first = null;
      last = null;
  
  }
  
  /*
   * Link class for creating Link nodes in the SortedLinkedList objects
   */
  private class Link&lt;E&gt; 
  {
      public Comparable&lt;E&gt; data;
      public Link next;
  
  }
  /*
   * Overiding add method from LinkedList class
   */
  public boolean add(E obj)
  {
      Link newLink = new Link();
      newLink.data = (Comparable&lt;E&gt;)obj;
  
      // When the list is initially empty
      if (first == null)
      {
          first = newLink;
          last = newLink;
          return true;
      }
  
  
      // When the element to be added is less than the first element in the list
      if (newLink.data.compareTo(first.data) &lt; 0)
      {
          //newLink.data = obj;
          newLink.next = first;
          first = newLink;
          return true;
      }
  
      // When the element to be added is greater than every element in in list
      // and has to be added at end of the list
      if (newLink.data.compareTo(last.data) &gt; 0)
      {
          //newLink.data = obj;
          last.next = newLink;
          last = newLink;
          return true;
      }
  
      //When the element to be added lies between other elements in the list
      if (newLink.data.compareTo(first.data) &gt;= 0 &amp;&amp; newLink.data.compareTo(last.data) &lt;= 0)
      {
          //newLink.data = obj;
          Link current = first.next;
          Link previous = first;
          while (newLink.data.compareTo(current.data) &lt;= 0)
          {
             previous = current;
             current = current.next;
          }
          previous.next = newLink;
          newLink.next = current;
  
      }
  
      return true;
  }
  
  
  public static void main (String[] args)
  {
      LinkedList&lt;Integer&gt; list = new SortedLinkedList&lt;Integer&gt;();
      list.add(4);
      list.add(5);
      list.add(10);
      list.add(9);
      //list.add(5);
      ListIterator&lt;Integer&gt; iterator = list.listIterator();
  
      while (iterator.hasNext())
      {
           System.out.println(iterator.next());
      }
  
  
  }
  </code></pre>
  
  <p>}</p>