Question:
The MyLinkedList class used in Listing 24.6 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows:
Implement a new class named TwoWayLinkedList that uses a doubly linked list to store elements. The MyLinkedList class in the text extends MyAbstractList. Define TwoWayLinkedList to extend the java.util.AbstractSequentialList class. You need to implement all the methods defined in MyLinkedList as well as the methods listIterator() and listIterator(int index). Both return an instance of java.util. ListIterator. The former sets the cursor to the head of the list and the latter to the element at the specified index.
Listing
Transcribed Image Text:
public class Node { E element; Node next; Node previous; public Node(E e) { element e; } 1 public class MyLinkedList extends MyAbstractlist { 2 private Node head, tail; /** Create a default list */ public MyLinkedList() { /** Create a list from an array of objects */ public MyLinkedList(E[] objects) { super(objects); 10 11 12 13 /** Return the head element in the list */ public E getFirst() { if (size == 0) { return null; 14 15 16 17 18 19 20 else { return head.element; 21 22 23 /** Return the last element in the list */ public E getlast() { if (size == 0) { return null; 24 25 26 27 28 else { return tail.element; 29 30 31 32 /** Add an element to the beginning of the list */ public void addFirst(E e) { // Implemented in Section 24.4.3.1, so omitted here 33 34 35 36 37 38 /** Add an element to the end of the list */ public void addLast (E e) { // Implemented in Section 24.4.3.2, so omitted here 39 40 41 42 43 @Override /** Add a new element at the specified index * in this list. The index of the head element is 0 */ public void add(int index, E e) { // Implemented in Section 24.4.3.3, so omitted here 44 45 46 47 48 49 50 51 52 /** Remove the head node and return the object that is contained in the removed node. */ public E removeFirst() { // Implemented in Section 24.4.3.4, so omitted here 53 54 HN3 4567