title: Java Ordered Doubly Linked List Program
body: <p>I'm having a problem a homework assignment.  The assignment is:</p>
  
  <p>Implement a class for an ordered, doubly linked list class.  It should be iterable.  That is, it should have an iterator( ) method that returns an object of type Iterator.  You should also implement the iterator itself.  If necessary create another class.</p>
  
  <p>In addition to this class(es) implement a driver to test it.  The driver should read in the name of a file containing character strings, one per line.  It should then read in the file, adding each string to the list.  Then use the iterator to print out the resulting list, which should be the same as the file contents in sorted order.</p>
  
  <p>So far I have all parts of the program compiling properly but I can't get the driver working correctly.  The code that I have so far is:</p>
  
  <pre><code>import java.util.Iterator;
  
  public interface ListADT&lt;T&gt; extends Iterable&lt;T&gt;
  {
      /**
       * Removes and returns the first element from this list.
       */
      public T removeFirst( );
  
      /**
       * Removes and returns the last element from this list.
       */
      public T removeLast( );
  
      /**
       * Removes and returns the specified element from this list.
       */
      public T remove(T element);
  
      /**
       * Returns a reference to the first element in this list.
       */
      public T first( );
  
      /**
       * Returns a reference to the last element in this list.
       */
      public T last( );
  
      /**
       * Returns true if this list contains the specified target element.
       */
      public boolean contains(T target);
  
      /**
       * Returns true if this list contains no elements.
       */
      public boolean isEmpty( );
  
      /**
       * Returns the number of elements in this list.
       */
      public int size( );
  
      /**
       * Returns an iterator for the elements in this list.
       */
      public Iterator&lt;T&gt; iterator( );
  
      /**
       * Returns a string representation of this list.
       */
      public String toString( );
  }
  
  public interface OrderedListADT&lt;T&gt; extends ListADT&lt;T&gt;
  {
     /**
      * Adds the specified element to this list at the proper location.
      */
     public void add(T element);
  }
  
  import java.util.Iterator;
  
  public class DoubleList&lt;T&gt; implements ListADT&lt;T&gt;
  {
      protected DoubleNode&lt;T&gt; front, rear;
      protected int count;
  
      /**
       * Creates an empty list using the default capacity.
       */
      public DoubleList( )
      {
          rear = null;
          front = null;
          count = 0;
      }
  
      /**
       * Removes and returns the first element in the list.
       */
      public T removeFirst( ) throws EmptyCollectionException
      {
          if (isEmpty( ))
          {
              throw new EmptyCollectionException("list");
          }
  
          T result = front.getElement( );
          front = front.getNext( );
  
          if (front == null)
          {
              rear = null;
          }
          else
          {
              front.setPrevious(null);
          }
  
          count--;
          return result;
      }
  
      /**
       * Removes and returns the last element in the list.
       */
      public T removeLast( ) throws EmptyCollectionException
      {
          T result;
  
          if (isEmpty( ))
          {
              throw new EmptyCollectionException("list");
          }
  
          result = rear.getElement( );
          rear = rear.getPrevious( );
  
          if (rear == null)
          {
              front = null;
          }
          else
          {
              rear.setNext(null);
          }
  
          count--;
  
          return result;
      }
  
      /**
       * Removes and returns the specified element.
       */
      public T remove(T element) throws ElementNotFoundException
      {
          T result;
          DoubleNode&lt;T&gt; nodeptr = find(element);
  
          if (nodeptr == null)
          {
              throw new ElementNotFoundException("list");
          }
  
          result = nodeptr.getElement( );
  
          if (nodeptr == front)
          {
              result = this.removeFirst( );
          }
          else if (nodeptr == rear)
          {
              result = this.removeLast( );
          }
          else
          {
              nodeptr.getNext( ).setPrevious(nodeptr.getPrevious( ));
              nodeptr.getPrevious( ).setNext(nodeptr.getNext( ));
              count--;
          }
  
          return result;
      }
  
      /**
       * Returns a reference to the element at the front of the list.
       * The element is not removed from the list.  Throws an 
       * EmptyCollectionException if the list is empty.
       */
      public T first( ) throws EmptyCollectionException
      {
          if (isEmpty( ))
          {
              throw new EmptyCollectionException("list");
          }
  
          return front.getElement( );
      }
  
      /**
       * Returns a reference to the element at the rear of the list.
       * The element is not removed from the list.  Throws an
       * EmptyCollectionException if the list is empty.
       */
      public T last( ) throws EmptyCollectionException
      {
          if (isEmpty( ))
          {
              throw new EmptyCollectionException("list");
          }
  
          return rear.getElement( );
      }
  
      /**
       * Returns true if this list contains the specified element.
       */
      public boolean contains(T target)
      {
          return (find(target) != null);
      }
  
      /**
       * Returns a reference to the specified element, or null if it
       * is not found.
       */
      public DoubleNode&lt;T&gt; find(T target)
      {
          boolean found = false;
          DoubleNode&lt;T&gt; traverse = front;
          DoubleNode&lt;T&gt; result = null;
  
          if (! isEmpty( ))
          {
              while (! found &amp;&amp; traverse != null)
              {
                  if (target.equals(traverse.getElement( )))
                  {
                      found = true;
                  }
                  else
                  {
                      traverse = traverse.getNext( );
                  }
              }
          }
          if (found)
          {
              result = traverse;
          }
  
          return result;
      }
  
      /**
       * Returns true if this list is empty and false otherwise.
       */
      public boolean isEmpty( )
      {
          return (count == 0);
      }
  
      /**
       * Returns the number of elements currently in this list.
       */
      public int size( )
      {
          return count;
      }
  
      /**
       * Returns an iterator for the elements currently in this list.
       */
      public Iterator&lt;T&gt; iterator( )
      {
          return new DoubleIterator&lt;T&gt;(front, count);
      }
  
      /**
       * Returns a string representation of this list.
       */
      public String toString( )
      {
          String result = "";
          DoubleNode&lt;T&gt; traverse = front;
  
          while (traverse != null)
          {
              result = result + (traverse.getElement( )).toString( ) + "\n";
              traverse = traverse.getNext( );
          }
  
          return result;
      }
  }
  
  public class DoubleOrderedList&lt;T&gt; extends DoubleList&lt;T&gt; implements OrderedListADT&lt;T&gt;
  {
      /**
       * Creates an empty list using the default capacity.
       */
      public DoubleOrderedList( )
      {
          super( );
      }
  
      /**
       * Adds the specified element after the specified target element.
       * Throws a ClassCastException if the element is not Comparable.
       * Throws a ElementNotFoundException if the target is not found.
       */
      public void add(T element)
      {
          Comparable temp;
  
          temp = (Comparable)element;
  
          DoubleNode&lt;T&gt; traverse = front;
          DoubleNode&lt;T&gt; newnode = new DoubleNode&lt;T&gt;(element);
          boolean found = false;
  
          if (isEmpty( ))
          {
              front = newnode;
              rear = newnode;
          }
          else if (temp.compareTo(rear.getElement( )) &gt;= 0)
          {
              rear.setNext(newnode);
              newnode.setPrevious(rear);
              newnode.setNext(null);
              rear = newnode;
          }
          else if (temp.compareTo(front.getElement( )) &lt;= 0)
          {
              front.setPrevious(newnode);
              newnode.setNext(front);
              newnode.setPrevious(null);
              front = newnode;
          }
          else
          {
              while ((temp.compareTo(traverse.getElement( )) &gt; 0))
              {
                  traverse = traverse.getNext( );
              }
  
              newnode.setNext(traverse);
              newnode.setPrevious(traverse.getPrevious( ));
              traverse.getPrevious( ).setNext(newnode);
              traverse.setPrevious(newnode);
          }
  
          count++;
      }
  }
  
  import java.util.*;
  
  public class DoubleIterator&lt;T&gt; implements Iterator
  {
      private int count; //The number of elements in the collection.
      private DoubleNode&lt;T&gt; current; //The current position
  
      /**
       * Sets up this iterator using the specified items.
       */
      public DoubleIterator(DoubleNode&lt;T&gt; list, int size)
      {
          current = list;
          count = size;
      }
  
      /**
       * Returns true if this iterator has at least one more element
       * to deliver in the iteration.
       */
      public boolean hasNext( )
      {
          return (current != null);
      }
  
      /**
       * Returns the next element in the iteration.  If there are
       * no more elements in this iteration, a NoSuchElementException
       * is thrown
       */
      public T next( )
      {
          if (! hasNext( ))
          {
              throw new NoSuchElementException( );
          }
  
          T result = current.getElement( );
          current = current.getNext( );
  
          return result;
      }
  
      /**
       * The remove operation is not supported.
       */
      public void remove( ) throws UnsupportedOperationException
      {
          throw new UnsupportedOperationException( );
      }
  }
  
  public class DoubleNode&lt;E&gt;
  {
      private DoubleNode&lt;E&gt; next;
      private E element;
      private DoubleNode&lt;E&gt; previous;
  
      /**
       * Creates an empty node.
       */
      public DoubleNode( )
      {
          next = null;
          element = null;
          previous = null;
      }
  
      /**
       * Creates a node storing the specified element.
       */
      public DoubleNode(E elem)
      {
          next = null;
          element = elem;
          previous = null;
      }
  
      /**
       * Returns the node that follows this one.
       */
      public DoubleNode&lt;E&gt; getNext( )
      {
          return next;
      }
  
      /**
       * Returns the node that precedes this one.
       */
      public DoubleNode&lt;E&gt; getPrevious( )
      {
          return previous;
      }
  
      /**
       * Sets the node that follows this one.
       */
      public void setNext(DoubleNode&lt;E&gt; dnode)
      {
          next = dnode;
      }
  
      /**
       * Sets the node that follows this one.
       */
      public void setPrevious(DoubleNode&lt;E&gt; dnode)
      {
          previous = dnode;
      }
  
      /**
       * Returns the element stored in this node.
       */
      public E getElement( )
      {
          return element;
      }
  
      /**
       * Sets the element stored in this node.
       */
      public void setElement(E elem)
      {
          element = elem;
      }
  }
  
  
  import java.util.*;
  import java.io.*;
  
  public class Driver
  {
      public static void main(String[ ] args) throws IOException
      {
          Scanner keyboard = new Scanner(System.in); //Variable used for keyboard input.
          String entry; //Variable used for lines of text in the file.
  
          //Ask the user for the name of the file they want to use.
          System.out.print("Enter the name of the file: ");
          String filename = keyboard.nextLine( );
  
          //Check to see if the file exists.
          File file = new File(filename);
  
          //Runs if the filename entered is invalid or does not exist.
          if (!file.exists( ))
          {
              //Displays an error message if the file does not exist.
              System.out.println("The file " + filename + " does not exist.");
  
              //Exits the program.
              System.exit(0);
          }
  
          //Opens the file.
          Scanner inputFile = new Scanner(file);
  
          while (inputFile.hasNext( ))
          {
              entry = inputFile.nextLine( );
              DoubleOrderedList&lt;String&gt; doublyOrderedList = new DoubleOrderedList&lt;String&gt;( );
  
              doublyOrderedList.add(entry);
              doublyOrderedList.iterator( );
  
  
          }
  
          //Close the file when all processes have finished.
          inputFile.close( );
      }
  }
  </code></pre>
  
  <p>The problem that I'm having is getting the driver to print out the list after it has been sorted.  I'm not an experienced or very good programmer but I have tried every possible option I can think of including asking for help from classmates who are rather good at programming.  I've also asked multiple times on the programmers page and they were unwilling to help me with my problem.  I'm not asking anyone to do the work for me I'm just exhausted with trying to figure this out and could use a fresh set of eyes on this.</p>