The MyLinkedList class is a one-way directional linked list that enables one-way
ID: 3651959 • Letter: T
Question
The MyLinkedList class is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new field name previous to refer to the previous node in the list, as followspublic class Node<E> {
E element;
Node <E> next;
Node<E> previous;
public Node (E o) {
element = o;
}
}
Implement a new class named MyTwoWayLinkedList that uses a doubly linked list to store elements
Hint: You may define MyTwoWayLinkedList to extend the java.util.AbstractSequentialList class. You need to implement the methods listIterator () and listIterator (int index). Both return an instance of java.util.ListIterator<E>. The former sets the cursor to the head of the list and the latter to the element at the specified index.
Explanation / Answer
public class TreeNode { Object element; TreeNode next; TreeNode previous; public TreeNode(Object o) { element = o; } } Simplify the implementation of the add(Object element, int index) and remove(int index) methods to take advantage of the bi-directional linked list.