Steps in doing the homework:
  1. Get the code from
    /u/dgerman/hw7
    and put it in
    /u/yourUsername/httpd/htdocs/hw7
  2. Write the append function.

  3. Test it from
    http://yourHost.cs.indiana.edu:yourPortNumber/hw7/list.html
Cases to think about for append

Data structure for a LinkedList object:
Fig. 1 Status of a LinkedList instance after append is executed 3 times followed by one call to nextElement (note that pre is now pointing to the first element, so the current element is 2)

Let's append one more element now.

We will do this in stages.

(Note: please read with care what follows!)

How about if we do it this way

  1. modify the reference for the tail of the LinkedList (that is step 1 in the figure below)

    and then

  2. make the last element point to the new element Well, you can do it this way but the question now is:
    How do you get to the last element if your tail points to the new cell?

    One way would be to traverse the entire list etc. and when you reach the last element put it's .next value point to the new cell.

    A better way is to do step 2 before you do step 1 without being confused about the the way they are numbered (which was for the sake of isolating the wrong order).

    Fig. 2 A new append

    Do not forget to update len.

    Here's the distributed code for LinkedList


    public class LinkedList { 
    
      private Link head; 
      private Link tail; 
      private Link pre; // predecessor of cursor 
      // note: the cursor is fictitious (or implicit)
      int len;  
    
      public void reset() { 
        // reset the cursor 
        pre = null; 
      } 
    
      public boolean hasMoreElements() { 
        // return true iff (if and only if) the 
        // cursor is not at the end of the list 
        return cursor() != null; 
      } 
    
      public Object nextElement() { 
        // - move the cursor to the next position
        // - return the current element (before
        //   advancing to the next position)
        // - throw NoSuchElementException if already
        //   at the end of list  
        if (pre == null) { pre = head; } 
        else pre = pre.next; 
    
        if (pre.next == null) pre = pre.next; 
    
        if (pre == null && head == null) throw new java.util.NoSuchElementException(); 
        else if (head != null) return head.data; 
        else return pre.data; 
      } 
    
      public Object currentElement () {
        // - return the current element under the cursor 
        // - throw NoSuchElementException if already at the
        //   end of the list 
        Link cur = cursor(); 
        if (cur == null) throw new java.util.NoSuchElementException(); 
        return cur.data; 
      }
    
      public void insert (Object n) {
        // - insert before the cursor 
        // - n is the object to insert
        Link p = new Link(n, cursor()); 
        if (pre != null) {
          pre.next = p; 
          if (pre == tail) tail = p; 
        } else {
          if (head == null) tail = p; 
          head = p; 
        } 
        pre = p; // this should probably
                 // go inside the else branch
                 // given the semantics of pre 
                 // as noted by Dave Waicukauski
        len ++; 
      } 
    
      public void append(Object n) {
        // - insert after the tail of the list 
        // - n is the object to insert 
    
    
        // write this method 
    
    
    
      } // end of append 
    
      public Object remove() {
        // - remove the element under the cursor 
        // - return the removed element 
        // - throw NoSuchElementException if already
        //   at the end of the list 
        Link cur = cursor(); 
        if (cur == null) throw new java.util.NoSuchElementException(); 
        if (tail == cur) tail = pre; 
        if (pre != null) pre.next = cur.next;
        else head = cur.next;
        len--;
        return cur.data; 
      } 
    
      int size() { 
        // return the number of elements in the list 
        return len; 
      } 
    
      public java.util.Enumeration elements() {
        // - return an enumeration to iterate 
        //   through all elements in the list
        return new ListEnumeration(head); 
      } 
    
      private Link cursor() {
        if (pre == null) return head; 
        else return pre.next; 
      }
    
    }
    
    class Link {
      Object data; Link next; 
      Link (Object d, Link n) { 
        data = d; next = n; 
      } 
    }   
    
    class ListEnumeration implements // the interface 
                          java.util.Enumeration {
      private Link cursor; // let's call this iterator (here) 
      public ListEnumeration (Link l) { 
        // constructor (from the main of TestList 
        // we call this with ListEnumeration(head)
        cursor = l; 
      } 
      public boolean hasMoreElements() { 
        // return true iff the iterator 
        // is not at the end of the list 
        return cursor != null; 
      } 
      public Object nextElement() {
        // - move the iterator to the next position 
        // - return the current element (before advancing to the position)
        // - throw NoSuchElementException if already at the end of list 
    
        if (cursor == null) throw new java.util.NoSuchElementException(); 
        Object r = cursor.data;
        cursor = cursor.next; 
        return r; 
      } 
    }