Question: ( Implement a doubly linked list ) The MyLinkedList class used in Listing 24.6 (This is in the book Introduction to Java Programming 10th Edition)

(Implement a doubly linked list) The MyLinkedList class used in Listing 24.6 (This is in the book Introduction to Java Programming 10th Edition) 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:

public class Node { E element; Node next; Node previous;

public Node(E e) {

element = e;

} }

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.

Write a main method that tests your implementation of the doubly linked list by doing the following:

1) Create a new TwoWayLinkedList and use the add() operator to add the numbers 1, 2, 3, and 4 to the list.

2) Print the list in order using toString()

3) Advance to the last element in the list using the next() operator

4) Print the list in reverse order using the previous() and hasPrevious() methods

5) Print the elements starting with index 2 using the listIterator(int index) method.

Your output should look like that below:

Printing the list in order using toString(): [1, 2, 3, 4]

Advancing to the last element in the list using the next() operator.

Printing the list in reverse order using the previous() and hasPrevious() methods:

4 3 2 1

Printing elements starting with index 2 using listIterator(int index):

3 4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!