<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * A linked list implementation of List using lots of iteration instead of recursion.
 * 
 * @author Paul Chew for CS211, Sep 2006.
 */
public class LinkedListIterative implements List {
    
    ListCell head;      // Head (first cell) of the List
    
    /**
     * Constructor.
     */
    public LinkedListIterative () {
        head = null;
    }
            
    /**
     * Insert an element onto the list.
     * @param element the element to insert
     */
    public void insert (Object element) {
        head = new ListCell(element, head);
    }
    
    /**
     * Delete an element from the list.
     * @param element the element to delete
     */
    public void delete (Object element) {
        if (head == null) return;
        if (head.datum.equals(element)) {
            head = head.next;
            return;
        }
        ListCell current = head;
        ListCell scout = head.next;
        while ((scout != null) &amp;&amp; !scout.datum.equals(element)) {
            current = scout;
            scout = scout.next;
        }
        if (scout != null) current.next = scout.next;
        return;
    }
    
    /**
     * Report true if list contains element.
     * @param element the element to check for
     * @return true iff element is in the List
     */
    public boolean contains (Object element) {
        for (ListCell current = head; current != null; current = current.next)
            if (current.datum.equals(element)) return true;
        return false;
    }

    /**
     * Report number of elements in List.
     * @return number of elements
     */
    public int size () {
        int count = 0;
        for (ListCell current = head; current != null; current = current.next) count++;
        return count;
    }
    
    /**
     * String representation.
     * @return the String representation
     */
    public String toString () {
        String result = "[";
        for (ListCell current = head; current != null; current = current.next)
            result += " " + current.datum.toString();
        return result + " ]";
    }
}
</pre></body></html>