Linked list code developed during the 2010 March 11 lecture in CSCD300

This is available as a .java file.

/**
 * Linked list code developed during the 2010 March 11 lecture in CSCD300.
 *
 * @author  Timothy Rolfe and CSCD300 class members present
 */
@SuppressWarnings("unchecked")
public class LinkedList
{
   static class Node implements Comparable
   {
      Comparable data;
      Node       next;

      private Node(Comparable d)
      {  this(d, null);  }

      private Node(Comparable d, Node n)
      {  data = d; next = n;  }

      public int compareTo(Object ob)
      {  return data.compareTo(((Node)ob).data);  }

      public String toString()
      {  return data.toString();  }
   }

   protected Node head = new Node(null, null),
                  tail = null;
   protected int  size = 0;

   public int size()  {  return size;  }

   public boolean isEmpty()
   {  return size == 0;  }

   public void clear()
   {  head.next = tail = null;  size = 0;  }

   public void addFirst(Comparable data)
   {
/*
      head.next = new Node(data, head.next);
      if (size++ == 0)
         tail = head.next;
*/
      add(0, data);
   }

   public void add(Comparable data)
   {
/*    if (size++ == 0)
         head.next = tail = new Node(data);
      else
         tail = tail.next = new Node(data);
      size++
*/
      add(size, data);
   }

   public void add(int index, Comparable data)
   {  Node prev = head;
      if (index < 0 || index > size)
         throw new RuntimeException("Size is " + size);
      while (index-- > 0)
         prev = prev.next;
      prev.next = new Node(data, prev.next);
      if (prev.next.next == null)
         tail = prev.next;
      size++;
   }

   public Comparable remove(int index)
   {  Node prev = head;
      Comparable hold;

      if (index < 0 || index > size-1)
         throw new RuntimeException("Size is " + size);
      while (index-- > 0)
         prev = prev.next;
      hold = prev.next.data;
      prev.next = prev.next.next;
   // ERROR caused by blind copy/paste
   // if (prev.next.next == null)
   //    tail = prev.next;
      if (prev.next == null)
         tail = prev;
      size--;
      return hold;
   }

   public Comparable get(int index)
   {  Node prev = head;
      if (index < 0 || index > size)
         throw new RuntimeException("Size is " + size);
      while (index-- > 0)
         prev = prev.next;
      return prev.next.data;
   }

   public void showBigger(Comparable data)
   {
      for (Node curr = head.next; curr != null; curr = curr.next)
         if (data.compareTo(curr.data) < 0)
            System.out.printf("%s ", curr.data);
         else
            ;
      System.out.println();
   }

   public void addOrdered(Comparable data)
   {  Node prev = head;

      while (prev.next != null && data.compareTo(prev.next.data) > 0 )
         prev = prev.next;
      prev.next = new Node(data, prev.next);
      if (prev.next.next == null)
         tail = prev.next;
      size++;
   }

   public String toString()
   {  StringBuilder s = new StringBuilder("[");
      Node current = head.next;

      if (current!=null)
      {  s.append(current); current = current.next;  }

      while (current != null)
      {  s.append(", " + current);
         current = current.next;
      }
      s.append("]");
      return s.toString();
   }

   public static void main(String[] args)
   {  java.util.Scanner console = new java.util.Scanner(System.in);
      String line;
      LinkedList list = new LinkedList();

      System.out.println("Empty string to end.");
      line = console.nextLine();
      while (line.length() > 0)
      {  list.add(line);
         line = console.nextLine();
      }

      System.out.println(list);
      try
      {  list.remove(-1);  }
      catch (Exception e) {  System.out.println(e);  }
      try
      {  list.remove(list.size());  }
      catch (Exception e) {  System.out.println(e);  }

      System.out.println(list.remove(0));
      System.out.println(list.remove(list.size()-1));
      System.out.printf("From %d:  %s\n", list.size()/2, list.remove(list.size()/2));
      System.out.println(list);
   }
}