Needs to be typed in JAVA: Implement a doubly linked list ) The MyLinkedList cla
ID: 3864585 • Letter: N
Question
Needs to be typed in JAVA:
Implement a doubly linked list) 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:
public class Node<E> {
E element;
Node<E> next;
Node<E> 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<E>. The former sets the cursor to the head of the list and the
latter to the element at the specified index. Test using this code:
TEST USING THIS CODE:
USE PUBLIC CLASS EXERCISE2 for the name of the public class......
please include picture of sample run
HINT: The input for this test is
1 2 3 4 5
The expected output pattern is
1 2 3 4 5 5 4 3 2 1
Explanation / Answer
import java.util.Iterator; public class Exercise03 { public static void main(String[] args) { MyTwoWayLinkedList list = new MyTwoWayLinkedList(); list.add("asdf"); list.add("1234"); list.add("fffff"); list.add("44556699"); list.add("1234"); list.add("ccsdcd"); list.add("1234"); list.add("1234sssss"); System.out.println(list); list.remove(3); System.out.println(list); list.removeLast(); System.out.println(list); System.out.println(list.contains("asd")); System.out.println(list.contains("fffff")); System.out.println(); System.out.println(list.get(3)); System.out.println(list.get(5)); System.out.println(); System.out.println(list.indexOf("44556699")); System.out.println(list.indexOf("asdf")); System.out.println(list.indexOf("123")); System.out.println(list.indexOf("1234")); System.out.println(); System.out.println(list.lastIndexOf("44556699")); System.out.println(list.lastIndexOf("1234")); System.out.println(list.lastIndexOf("1234sssssssss")); System.out.println(); System.out.println(list.set(0, "987654321")); System.out.println(list.set(5, "tratata")); System.out.println(list); for (Iterator iterator = list.iterator(); iterator.hasNext();) { iterator.remove(); } System.out.println(list); } } class MyTwoWayLinkedList extends MyAbstractList { private Node head, tail; /** Create a default list */ public MyTwoWayLinkedList() { } /** Create a list from an array of objects */ public MyTwoWayLinkedList(E[] objects) { super(objects); } /** Return the head element in the list */ public E getFirst() { if (size == 0) { return null; } else { return head.element; } } /** Return the last element in the list */ public E getLast() { if (size == 0) { return null; } else { return tail.element; } } /** Add an element to the beginning of the list */ public void addFirst(E e) { Node newNode = new Node(e); // Create a new node newNode.next = head; // link the new node with the head newNode.previous = null; head = newNode; // head points to the new node size++; // Increase list size if (tail == null) // the new node is the only node in list tail = head; } /** Add an element to the end of the list */ public void addLast(E e) { Node newNode = new Node(e); // Create a new for element e if (tail == null) { head = tail = newNode; // The new node is the only node in list } else { tail.next = newNode; // Link the new with the last node newNode.previous = tail; newNode.next = null; tail = tail.next; // tail now points to the last node } size++; // Increase size } @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) { if (index == 0) { addFirst(e); } else if (index >= size) { addLast(e); } else { Node current = head; for (int i = 1; i = size) { return null; } else if (index == 0) { return removeFirst(); } else if (index == size - 1) { return removeLast(); } else { Node previous = head; for (int i = 1; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.