Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

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.

 

 

 

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY