Question: /** The DLinkedList class implements a doubly Linked list. */ class DLinkedListTemplate { /** The Node class stores a list element and a reference to
/** The DLinkedList class implements a doubly Linked list. */
class DLinkedListTemplate { /** The Node class stores a list element and a reference to the next node. */ private class Node { String value; // Value of a list element Node next; // Next node in the list Node prev; // Previous element in the list /** Constructor. @param val The element to be stored in the node. @param n The reference to the successor node. @param p The reference to the predecessor node. */ Node(String val, Node n, Node p) // element,successor,predecessor /** Constructor. @param val The element to be stored in the node. */ Node(String val) { // Just call the other (sister) constructor this(val, null, null); } } // Head of the list // Last element on the list /** Constructor. */ public DLinkedList() { } /** The isEmpty method checks to see if the list is empty. @return true if list is empty, false otherwise. */ public boolean isEmpty() /** The size method returns the length of the list. @return The number of elements in the list. */ public int size() // There is an element at p /** The add method adds to the end of the list. @param e The value to add. */ public void add(String e) { // Add to end of existing list } } /** This add method adds an element at an index. @param e The element to add to the list. @param index The index at which to add. @exception IndexOutOfBoundsException When the index is out of bounds. */ public void add(int index, String e) { // Index is at least 0 // New element goes at beginning // Old first } // pred will point to the predecessor // of the new node. // Splice in a node with the new element // We want to go from pred-- succ to // pred--middle--succ /** The toString method computes the string representation of the list. @return The string representation of the linked list. */ public String toString() { StringBuilder strBuilder = new StringBuilder(); // Use p to walk down the linked list /** The remove method removes the element at a given position. @param index The position of the element to remove. @return The element removed. @exception IndexOutOfBoundsException When index is out of bounds. */ public String remove(int index) { // Locate the node targeted for removal // Element to return // Node before the target // Node after the target // Route forward and back pointers around // the node to be removed /** The remove method removes an element from the list. @param element The element to remove. @return true if the element was removed, false otherwise. */ public boolean remove(String element) // Locate the node targeted for removal Node pred = target.prev; // Node before the target Node succ = target.next; // Node after the target // Route forward and back pointers around // the node to be removed public static void main(String [] args) { DLinkedList ll = new DLinkedList(); ll.add("Amy"); ll.add("Bob"); ll.add(0, "Al"); ll.add(2, "Beth"); ll.add(4, "Carol"); System.out.println("The elements of the list are:"); System.out.println(ll); } }
(Directions are in the comments of the programs, please write in JAVA)
thank you,
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
