Question: public interface LinkedList { /** * @return This method returns the head of the list */ Node getHead(); /** * @return This method returns the
public interface LinkedList { /** * @return This method returns the head of the list */ Node getHead(); /** * @return This method returns the number of elements stored in this list * */ int getCount(); /** * @return This method returns the node at the given index * */ Node get(int index) throws IndexOutOfBoundsException; /** * This method should set the value at the given location * @param index the index of the location where the new value to be set * @param newValue the new value * @return the old value at the index * @throws IndexOutOfBoundsException Throws the exception when the index is out of bound */ String set(int index, String newValue); /** * This method should add the given value to the end of the list * @param value This is the value to be added */ void append(String value); /** * This method should insert the given value after the given node. * (We assume that the given node is in the list.) * * @param node This is the node after which the value should be inserted * @param value This is the value to be inserted */ void insertAfter(Node node, String value) throws IllegalArgumentException; /** * This method should remove the given node in the list * (We assume that the given node is in the list.) * * All the values stored at/after the given index should be shifted forward by one index * For example, if the array has 3 elements stored at index 0, index 1, index 2 * if we remove the element at index 0, the elements at index 1 and index 2 should be moved to index 0 and index 1 * @param node the node to be removed * @return the value of the removed node */ String remove(Node node) throws IllegalArgumentException; }
public class DoublyLinkedList implements LinkedList { private DoublyLinkedListNode head; // the reference that points to the first node; is NULL if the list is empty private DoublyLinkedListNode tail; // the reference that points to the last node; is NULL if the list is empty private int count; // the count of the elements that the list stores public Node getHead() {return this.head;} public DoublyLinkedListNode getTail() {return this.tail;} /** * the class to represent Node in the list * DO NOT change this inner class * */ public static class DoublyLinkedListNode extends Node { DoublyLinkedListNode next; DoublyLinkedListNode prev; public DoublyLinkedListNode(String value){ super(value); this.next = null; this.prev = null; } public DoublyLinkedListNode getPrev() { return this.prev; } @Override public DoublyLinkedListNode getNext() { return this.next; } } /** * DO NOT change this constructor * This constructor initialize the instance variables */ public DoublyLinkedList(){ count = 0; head = null; tail = null; } /** * DO NOT change this method * @return This method returns the current number of elements stored in this list * */ @Override public int getCount() {return this.count;} /* Todo: implement this method */ @Override public Node get(int index) throws IndexOutOfBoundsException { return null; } /* Todo: implement this method */ @Override public String set(int index, String newValue) { return null; } /* Todo: implement this method */ @Override public void append(String value) { } /* Todo: implement this method */ @Override public void insertAfter(Node node, String value) throws IllegalArgumentException { } /* Todo: implement this method */ @Override public String remove(Node node) throws IllegalArgumentException { return null; } }
public abstract class Node { private String value; public String getValue() {return value;} public void setValue(String newValue) {this.value = newValue;} public Node(String value) { this.value = value; } public abstract Node getNext(); } Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
