title: Iterator for a linkedlist
body: <p>My project should implement two classes. A basic linked list and a sorted linked list. Everything seems to be working fine except for some reason I can't iterate through the sorted linked list. The class structure is as follows:</p>
  
  <pre><code>public class BasicLinkedList&lt;T&gt; implements Iterable&lt;T&gt; {
      public int size;
  
      private class Node {
          private T data;
          private Node next;
  
          private Node(T data) {
              this.data = data;
              next = null;
          }
      }
  
      private Node head;
      private Node tail;
  
      public BasicLinkedList() {
          head = tail = null;
      }
  //Add, remove method 
  
  public Iterator&lt;T&gt; iterator() {
          return new Iterator&lt;T&gt;() {
  
              Node current = head;
  
              @Override
              public boolean hasNext() {
                  return current != null;
              }
  
              @Override
              public T next() {
                  if(hasNext()){
                      T data = current.data;
                      current = current.next;
                      return data;
                  }
                  return null;
              }
  
              @Override
              public void remove(){
                  throw new UnsupportedOperationException("Remove not implemented.");
              }
  
          };
  </code></pre>
  
  <p>Now when I test this class it works just fine. The iterator works and I can test it all. The problem is in the sorted linked list class which extends this one. Here's its implementation and a comparator class that I'm using in the constructor:</p>
  
  <pre><code>public class SortedLinkedList&lt;T&gt; extends BasicLinkedList&lt;T&gt;{
      private class Node{
          private T data;
          private Node next;
  
          private Node(T data){
              this.data = data;
              next = null;
          }
      }
  
      private Node head;
      private Node tail;
      private Comparator&lt;T&gt; comp;
  
      public SortedLinkedList(Comparator&lt;T&gt; comparator){
          super();
          this.comp = comparator;
      }
  </code></pre>
  
  <p>Here's the comparator class and the test I ran in a separate class:</p>
  
  <pre><code>public class intComparator implements Comparator&lt;Integer&gt;{
          @Override
          public int compare(Integer o1, Integer o2) {
              return o1 - o2;
          }
  }
  
  public static void main(String[] args) {
  System.out.println("---------------SortedLinkedList--------------");
          SortedLinkedList&lt;Integer&gt; sortedList = new SortedLinkedList&lt;Integer&gt;(new intComparator());
          sortedList.add(3);
          sortedList.add(5);
          sortedList.add(2);
          for(int i: sortedList){
              System.out.println(i);
          }
      }   
  </code></pre>
  
  <p>Nothing prints out. I assumed the iterator that was inherited would help me traverse this no problem and clearly its legal because the for-each loop compiles. It's just that nothing gets printed out. I debugged it and all the adding, removing stuff works as expected. It's just that the iterator isn't doing what it's supposed to. Should I create a separate new iterator for this class? But wouldn't that be redundant code since I already inherit it? Help appreciated!</p>
  
  <p>EDIT: Here's the add method for the sorted list</p>
  
  <pre><code>public SortedLinkedList&lt;T&gt; add(T element){
          Node n = new Node(element);
          Node prev = null, curr = head;
          if(head == null){
              head = n;
              tail = n;
          }
          //See if the element goes at the very front
          else if(comp.compare(n.data, curr.data) &lt;= 0){
              n.next = head;
              head = n;
          }
          //See if the element is to be inserted at the very end
          else if(comp.compare(n.data, tail.data)&gt;=0){
              tail.next = n;
              tail = n;
          }
          //If element is to be inserted in the middle
          else{
              while(comp.compare(n.data, curr.data) &gt; 0){
                  prev = curr;
                  curr = curr.next;
              }
              prev.next = n;
              n.next = curr;
          }
  
          size++;
          return this;
      }
  </code></pre>