<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 recursion.
 * 
 * @author Paul Chew for CS211, Sep 2006.
 */
public class LinkedListRecursive implements List {
    
    ListCell head;      // Head (first cell) of the List
    
    /**
     * Constructor.
     */
    public LinkedListRecursive () {
        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) {
        head = delete(element, head);
    }
    
    private static ListCell delete (Object element, ListCell cell) {
        if (cell == null) return null;
        if (cell.datum.equals(element)) return cell.next;
        cell.next = delete(element, cell.next);
        return cell;
    }
    
    /**
     * 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) {
        return contains(element, head);
    }
    
    private static boolean contains (Object element, ListCell cell) {
        if (cell == null) return false;
        return cell.datum.equals(element) || contains(element, cell.next);
    }
    
    /**
     * Report number of elements in List.
     * @return number of elements
     */
    public int size () {
        return size (head);
    }
    
    private static int size (ListCell cell) {
        if (cell == null) return 0;
        return 1 + size(cell.next);
    }
    
    /**
     * String representation.
     * @return the String representation
     */
    public String toString () {
        return "[" + toString(head) + " ]";
    }
    
    private static String toString (ListCell cell) {
        if (cell == null) return "";
        return " " + cell.datum.toString() + toString(cell.next);
    }
}

</pre></body></html>