The MyLinkedList class is a one-way directional linked list that enables one-way traversal of the list....

80.2K

Verified Solution

Question

Programming

The MyLinkedList class is a one-way directional linked list thatenables one-way traversal of the list. Modify the Node class to addthe new field name previous to refer to the previous node in thelist, as follows:

    public class Node {

      E element;

Node next;

      Nodeprevious;

      public Node(E o){

        element =o;

      }

    }

Implement a new class named MyTwoWayLinkedList that uses adoubly linked list to store elements.

Hint. The MyLinkedList class in the textextends MyAbstractList. You may define MyTwoWayLinkedList to extendthe java.util.AbstractSequentialList class. You need to implementthe methods listIterator() and listIterator(int index). Both returnan instance of java.util.ListIterator. The former sets the cursorto the head of the list and the latter to the element at thespecified index.

Answer & Explanation Solved by verified expert
3.9 Ratings (620 Votes)
Program Plan Create class doublyLinkedList Create a static inner class TwoWayLinkedList which inherits from AbstractSequentialList class inside the doublyLinkedList class Create an object myList of type TwoWayLinkedList Add elements in the list myList by calling methods add addFirst and addLast Remove elements from the list myList by calling methods remove removeFirst and removeLast Create an object myIterator of type ListIterator and initialize it with the value returned on calling iterator method of myList Traverse the list myList from beginning to end and then vice versa In static inner class TwoWayLinkedList Create a private inner class LinkedListIterator which implements the ListIterator class Override method getFirst to return the first element of the list myList Override method getLast to return the last element of the list myList Override method addFirst to add an element at the beginning of the list myList Override method addLast to add an element at the end of the list myList Override method add to add element at the specified index Override method removeFirst to remove an element from the beginning of the list myList Override method removeLast to remove an element from the end of the list myList Override method remove to remove an element from the specified index in the list myList Override the toString method to store the content of the list in a string Override the clear method to empty the list Override the contains method to check whether the specified object is in the list or not Override the get method to return the element at the specified index Override the indexOf method to return the index of the element passed to it Override the lastIndexOf method to return the index at which the last occurrence of element is Override the set method so that a new element is placed at the place of the element present at the specified index Override the size method to return the size of the list Override the iterator method so that it returns the object of LinkedListIterator Create an inner class LinkedListIterator Create an inner class Node In inner class LinkedListIterator Define the constructor such that the iterator points at the specified index Override the hasNext method which returns false if the current node is null Override the next method so that it returns the current element and make the iterator point to the next element Override the remove method so that it removes the current element Override the hasPrevious element so that it returns false if the current node does not have predecessor Override the nextIndex method so that it returns the index of the next element Override the previousIndex method so that it returns    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students