The linked list class of the standard library has an add method that allows efficient insertion at the end of the list. Implement this method for the LinkedList class in Section 15.2. Add an instance variable to the linked list class that points to the last node in the list. Make sure the other mutator methods update that variable.

Complete the following file:

Use the following file:

AddTester.java

/**
   A tester program for a revised version of the LinkedList class.
*/
public class AddTester
{  
   public static void main(String[] args)
   {  
      LinkedList names = new LinkedList();
      
      names.addFirst("Tom");
      names.addFirst("Harry");
      names.addFirst("Dick");

      names.add("Romeo");
      printList(names);
      System.out.println("Expected: Dick Harry Tom Romeo");

      // remove last element and check that add still works
      ListIterator iterator = names.listIterator();
      while (iterator.hasNext()) iterator.next();
      iterator.remove();
      names.add("Juliet");
      printList(names);
      System.out.println("Expected: Dick Harry Tom Juliet");

      iterator = names.listIterator();
      // add after last element and check that add still works
      while (iterator.hasNext()) iterator.next();
      iterator.add("Emily");
      names.add("Charlotte");
      printList(names);
      System.out.println("Expected: Dick Harry Tom Juliet Emily Charlotte");

      // remove everything and check that add still works
      while (names.listIterator().hasNext())
         names.removeFirst();
      names.add("Nina");
      printList(names);
      System.out.println("Expected: Nina");

      names.removeFirst();
      names.addFirst("Adam");
      names.add("Eve");
      printList(names);
      System.out.println("Expected: Adam Eve");
   }
   
   private static void printList(LinkedList list)
   {
      ListIterator iterator = list.listIterator();
      while (iterator.hasNext())
      {
         System.out.print(iterator.next() + " ");
      }
      System.out.println();
   }
}