Question: /** * A recursive linked list class with recursive methods. * @param The element type **/ public class LinkedListRec { private Node head; private static
/** * A recursive linked list class with recursive methods. * @param
private Node
private static class Node
private Node(E dataItem) { data = dataItem; next = null; }
/** * Creates a new node that references another node. */ private Node(E dataItem, Node
/** * Recursive method to insert a specified data object before the * first occurrence of another specified data object. If the object is * not found, then the item is inserted at the end of the list. * @param target the item that inData is to be inserted before * @param inData the item to be inserted * @param node the current node */ private void insertBefore(E target, E inData, Node
} private int size(Node
public int size() { return size(head); }
private String toString(Node
@Override public String toString() { return toString(head); }
private void replace(Node
public void replace(E oldObj, E newObj) { replace(head, oldObj, newObj); }
private void add(Node
public void add(E data) { if (head == null) { head = new Node<>(data); // List has 1 node. } else { add(head, data); } }
/** * Removes a node from a list. * @post The first occurrence of outData is removed. * @param head The head of the current list * @param pred The predecessor of the list head * @param outData The data to be removed * @return true if the item is removed * and false otherwise */ private boolean remove(Node
/** * Wrapper method for removing a node (in LinkedListRec). * @post The first occurrence of outData is removed. * @param outData The data to be removed * @return true if the item is removed, * and false otherwise */ public boolean remove(E outData) { if (head == null) { return false; } else if (head.data.equals(outData)) { head = head.next; return true; } else { return remove(head.next, head, outData); } } /** * Method to insert an object at a specified index * @param obj The object to be inserted * @param index the index */ public void insert(E obj, int index) { if (index < 0) { throw new IndexOutOfBoundsException(); } if (index == 0) { head = new Node<>(obj, head); } else { insert(obj, head, index - 1); } }
/** * Method to insert an object at a specified index * @param obj The object to be inserted * @param pred the node preceding the node at the current index * @param index the current index */ private void insert(E obj, Node
/** * Recursive method to search a LinkedListRec. * @param item the item being sought. * @param node the current node * @return true if the item being sought is found */ private boolean search(E item, Node
______________________________
public class LinkedListRecTest {
public static void main(String[] args) {
LinkedListRec
aListDouble.add(2.5);
aListDouble.add(3.4);
aListDouble.add(5.8);
aListDouble.add(8.2);
aListDouble.add(16.5);
System.out.println("The current double type linked list:");
System.out.println(aListDouble.toString());
aListDouble.insertAfter(2.5, 10.5);
System.out.println("The updated double type linked list: insert after 2.5:");
System.out.println(aListDouble.toString());
aListDouble.insertAfter(16.5, 1.5);
System.out.println("The updated double type linked list: insert after 16.5:");
System.out.println(aListDouble.toString());
aListDouble.insertAfter(8.2, 120.5);
System.out.println("The updated double type linked list: insert after 8.5:");
System.out.println(aListDouble.toString());
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
