mplement Two Way Linked List The MyLinkedList class is a one-way directional linked list that enables one-way traversal of the list. MyLinkedList.java is attached below. Modify the Node class to add the new field named previous to refer to the previous node in the list, as follows: public class Node { E element; Node next; Node previous; public Node(E e) { element = e; } } Implement a new class named MyTwoWayLinkedList that uses a double linked list to store elements. The MyLinkedList class in the text extends MyList. Define MyTwoWayLinkedList to extend the java.util.AbstractSequentialList class. You can find methods of AbstractSequentialList from this article: https://www.geeksforgeeks.org/abstractsequentiallist-in-java-with-examples/. Study MyLinkedList.java carefully to understand how it is structured, and where and what you need to modify/implement for MyTwoWayLinkedList. Since MyTwoWayLinkedList extends java.util.AbstractSequentialList which implements Iterable, it includes an iterator () method that returns an object of the iterator interface automatically. Read the article https://www.softwaretestinghelp.com/java/learn-to-use-java-iterator-with-examples/ for basics to use iterator. In order to iterate through MyTwoWayLinkedList, we need to implement the methods listIterator() and listIterator(int index). Both return an instance of java.util.ListIterator. The former sets the cursor to head of the list and the latter to the element at the specific index. public ListIterator listIterator() { return new TwoWayLinkedListIterator(); } public ListIterator listIterator(int index) { return new TwoWayLinkedListIterator(index); } Here, TwoWayLinkedListIterator is a private class similar to LinkedListIterator in MyLinkedList.java. It defines a custom iterator for TwoWayLinkedList. Make sure to implement methods previous(), previousIndex(), and hasPrevious() similar to next(), nextIndex(), and hasNext() methods in MyLinkedList class. All the methods dealing with add/remove may need to be modified to handle both next and previous fields. Implement a new class named TestMyTwoWayLinkedList to test your two-way linked list with Double as the concert type for the elements in the linked list.
Implement Two Way Linked List
The MyLinkedList class is a one-way directional linked list that enables one-way traversal of the list. MyLinkedList.java is attached below. Modify the Node class to add the new field named previous to refer to the previous node in the list, as follows:
public class Node <E> { E element;
Node<E> next; Node<E> previous;
public Node(E e) { element = e;
} }
Implement a new class named MyTwoWayLinkedList that uses a double linked list to store elements. The MyLinkedList class in the text extends MyList. Define MyTwoWayLinkedList to extend the java.util.AbstractSequentialList class. You can find methods of AbstractSequentialList from this article: https://www.geeksforgeeks.org/abstractsequentiallist-in-java-with-examples/. Study MyLinkedList.java carefully to understand how it is structured, and where and what you need to modify/implement for MyTwoWayLinkedList.
Since MyTwoWayLinkedList extends java.util.AbstractSequentialList which implements Iterable<E>, it includes an iterator () method that returns an object of the iterator interface automatically. Read the article https://www.softwaretestinghelp.com/java/learn-to-use-java-iterator-with-examples/ for basics to use iterator. In order to iterate through MyTwoWayLinkedList, we 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 head of the list and the latter to the element at the specific index.
public ListIterator<E> listIterator() { return new TwoWayLinkedListIterator();
}
public ListIterator<E> listIterator(int index) { return new TwoWayLinkedListIterator(index);
}
Here, TwoWayLinkedListIterator is a private class similar to LinkedListIterator in MyLinkedList.java. It defines a custom iterator for TwoWayLinkedList. Make sure to implement methods previous(), previousIndex(), and hasPrevious() similar to next(), nextIndex(), and hasNext() methods in MyLinkedList class.
All the methods dealing with add/remove may need to be modified to handle both next and previous fields.
Implement a new class named TestMyTwoWayLinkedList to test your two-way linked list with Double as the concert type for the elements in the linked list.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps