/** * A singly-linked list suitable for recursive implementation of methods * @author Edo Biagioni * @assignment ICS 211 assignment 9 * @date March 28, 2025 */ public class LinkedListRecursive<E> { // here, include the LinkedNode definition /** * A node in a singly-linked list * @author Edo Biagioni * @lecture ICS 211 Jan 27 or later * @date January 26, 2010 */ private static class LinkedNode<T> { private T item; private LinkedNode<T> next; /** * constructor to build a node with no successor * @param the value to be stored by this node */ private LinkedNode(T value) { item = value; next = null; } /** * constructor to build a node with specified (maybe null) successor * @param the value to be stored by this node * @param the next field for this node */ private LinkedNode(T value, LinkedNode<T> reference) { item = value; next = reference; } } // end of the LinkedNode definition // this is the start of the linked list. If the list is empty, it is null protected LinkedNode<E> head; /** * initializes an empty linked list */ public LinkedListRecursive() { super(); head = null; } /** * return the size of the list * @return the size of the list * @runtime O(n) */ public int size() { // TO DO: use a recursive helper method to count the number // of nodes in the list return -1; } /** * adds a value to the end of the list * @param the value to be added * @return true (the add always succeeds) */ public boolean add(E value) { // TO DO: use a recursive helper method to add the value at the end return true; } /** * adds a value to the list, in the given position * @param the position at which to add: 0 to add at the start * @param the value to be added * @throws IndexOutOfBoundsException if the index is less than 0 * or greater than the number of elements in the linked list */ public void add(int index, E value) { // TO DO: use a recursive helper method to add the value at the index } /** * removes a value from the given position in the list * @param the position at which to remove: 0 to remove the first element * @return the removed element * @throws IndexOutOfBoundsException if the index is less than 0 * or greater than or equal to the number of elements in * the linked list */ public E remove(int index) { // TO DO: use a recursive helper method to remove the value at the index return null; } /** * concatenates the elements of the linked list, separated by " ==> " * @return the string representation of the list */ public String toString() { // TO DO: use a recursive helper method to concatenate all the values return "TO DO: toString() is not implemented yet"; } /** * checks element equality with value.equals(node.item). * @return the number of times this element occurs in this list, or 0. */ public int numMatches(E value) { // TO DO: use a recursive helper method to count the number of // occurrences of values in the list return -1; } /** * @return the last value for which value.compareTo(node.item) > 0, or * @return null if value <= each of the items in the list */ public E lastGreater(Comparable<E> value) { // TO DO: use a recursive helper method to find and return the // last value in the list for which value.compareTo(node.item) > 0 return null; } /** * @return the first value for which value.compareTo(node.item) > 0, or * @return null if value <= each of the items in the list */ public E firstGreater(Comparable<E> value) { // TO DO: use a recursive helper method to find and return the // first value in the list for which value.compareTo(node.item) > 0 return null; } /** * @return an array containing only the even-numbered (0, 2, 4, 6, ...) * elements of the list. */ public E[] toEvenArray() { // TO DO: call size to get number of elements in the list, // then create an array with (size + 1) / 2 elements. // then use a recursive helper method to fill the array with // the even-numbered elements, and return that array. // this method counts for twice as much credit (20%) as the others return null; } }