<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package edu.cornell.cs.cs2110;

/**
 * A linked list implementation of List using lots of recursion.
 * 
 */
public class LinkedListRecursive&lt;T&gt; implements List&lt;T&gt; {

   ListCell&lt;T&gt; 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(T element) {
      head = new ListCell&lt;T&gt;(element, head);
   }

   /**
    * Delete an element from the list.
    * 
    * @param element
    *           the element to delete
    */
   public void delete(T element) {
      delete(element, head);
   }

   private ListCell&lt;T&gt; delete(T element, ListCell&lt;T&gt; 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(T element) {
      return contains(element, head);
   }

   private boolean contains(T element, ListCell&lt;T&gt; 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 int size(ListCell&lt;T&gt; cell) {
      if (cell == null) return 0;
      return size(cell.next) + 1;
   }

   /**
    * String representation.
    * 
    * @return the String representation
    */
   public String toString() {
      return "[" + toString(head) + " ]";
   }

   private String toString(ListCell&lt;T&gt; cell) {
      if (cell == null) return "";
      return " " + cell.datum.toString() + toString(cell.next);
   }
}
</pre></body></html>