Return to the lecture notes index

Lecture #7 (September 17, 2002)

Exam: One Week from Today

We spoke very briefly about the upcoming exam. It will be a "pen and paper" exam. And, it will be closed book.

The exam will emphasize the following:

Today's Class

During today's class, we extended the LinkedList class from last class to include more complicted methods, focusing on those that don't necessarily just operate on the head or tail.

The LinkedList Implementation

public class LinkedList
{
  private LinkedListNode head;
  private LinkedListNode tail;
  private LinkedListNode index;
  
  public class LinkedListException extends Exception
  {
  
    public LinkedListException (String msg)
    {
      super(msg);
    }
    
  }
  
  public LinkedList()
  {
    head = tail = index = null;
  }
  
  public void prepend (Comparable data)
  {
    head = new LinkedListNode (data, head);
    
    if (null == tail)
      tail = head;
      
    if (null == index)
      index = head;
  }
  
  public void append (Comparable data)
  {
    if (null == tail)
      head = index = tail = new LinkedListNode(data);
    else
    {
      tail.setNext (new LinkedListNode (data));
      tail = tail.getNext();
    }
  }
  
  public void resetIndex()
  {
    index = head;
  }
  
  public Comparable getIndex() throws LinkedListException
  {
    if (null == index)
      throw new LinkedListException ("Null index in getIndex()");
   
    return index.getData();
  }
  
  public void advanceIndex() throws LinkedListException
  {
    if ((index == null) || (null == index.getNext()))
      throw new LinkedListException ("Null index in advanceIndex()");
      
    index = index.getNext();
  }
  
    // This is gonna' be paiiiinnnnful
    public void reverseIndex() throws LinkedListException
    {
      LinkedListNode temp = index; // Mark index's old position
     
      if ( (null == index) || (index == head) )
        throw new LinkedListException ("Index is null or has no predecessor");
      
      resetIndex();
    
      while (index.getNext() != temp)
        index = index.getNext();
      
      // Index stopped one before old position -- so done!
    }
  
    public Comparable deleteAtIndex() throws LinkedListException
    {
    
      Comparable data;
      
      try 
      {       
        data = index.getData();
        reverseIndex();
        // data = index.getNext().getData(); // Ugly, but avoids NPE
        index.setNext(index.getNext().getNext());
      }
      catch (NullPointerException npe)
      {
        throw new LinkedListException ("index is null; can't delete");
      }
      catch (LinkedListException lle)
      {
         // No predecessor; first node in list
        head = index.getNext();
      }
      
      // Index was the tail
      if (index.getNext() == null)
      {
        tail = index;
      }           
      
      // Move index to the one after the one we're deleting
      index = index.getNext();   
      
      return data; 
    }

    public Comparable removeNth (int n) // beginning with 0th 
      throws LinkedListException
    {
      LinkedListNode index; // Don't destroy user's index
      int count;
      
      if (null == head)
        throw new LinkedListException ("Can't delete from empty list");
      
      if (n == 0)
      { 
        if (tail == head)
          tail = null;
          
        if (index == head)
          index = null;
                
        head = head.getNext();
        
      }
      else
      {      
        for (index=head, count=0; count < n; count++, index=index.getNext())
        ;
      
        if (this.index == index.getNext())
          this.index = null;
        
        if (tail == index.getNext())
          tail = index;
         
        index.setNext(index.getNext().getNext());
      
     }
                          
    }
    
 } /* LinkedList */