Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In the existing DoublyLinkedList class, write and test a non - static method named reverseList to reverse a doubly linked list. Write the testing code
In the existing DoublyLinkedList class, write and test a nonstatic method named reverseList to reverse a doubly linked list. Write the testing code in the main method of the class DoublyLinkedList. For this purpose, you must only use and update the DoublyLinkedList.java file provided:
package linkedpublic class DoublyLinkedList
nested Node class
Node of a doubly linked list, which stores a reference to its
element and to both the previous and next node in the list.
private static class Node
The element stored at this node
private E element; reference to the element stored at this node
A reference to the preceding node in the list
private Node prev; reference to the previous node in the list
A reference to the subsequent node in the list
private Node next; reference to the subsequent node in the list
Creates a node with the given element and next node.
@param e the element to be stored
@param p reference to a node that should precede the new node
@param n reference to a node that should follow the new node
public NodeE e Node p Node n
element e;
prev p;
next n;
public E getElement return element;
public Node getPrev return prev;
public Node getNext return next;
public void setPrevNode p prev p;
public void setNextNode n next n;
private Node header;
private Node trailer;
private int size ;
public DoublyLinkedList
header new Nodenull null, null;
trailer new Nodenull header, null;
header.setNexttrailer;
public int size return size;
public boolean isEmpty return size ;
public E first
if isEmpty return null;
return header.getNextgetElement;
public E last
if isEmpty return null;
return trailer.getPrevgetElement;
public void addFirstE e
addBetweene header, header.getNext;
public void addLastE e
addBetweene trailer.getPrev trailer; place just before the trailer
public E removeFirst
if isEmpty return null;
return removeheadergetNext;
public E removeLast
if isEmpty return null;
return removetrailergetPrev;
private void addBetweenE e Node predecessor, Node successor
create and link a new node
Node newest new Nodee predecessor, successor;
predecessor.setNextnewest;
successor.setPrevnewest;
size;
private E removeNode node
Node predecessor node.getPrev;
Node successor node.getNext;
predecessor.setNextsuccessor;
successor.setPrevpredecessor;
size;
return node.getElement;
public String toString
StringBuilder sb new StringBuilder;
Node walk header.getNext;
while walk trailer
sbappendwalkgetElement;
walk walk.getNext;
if walk trailer
sbappend;
sbappend;
return sbtoString;
main method
public static void mainString args
create and populate a doubly linked list
DoublyLinkedList list new DoublyLinkedList;
list.addFirstMSP;
list.addLastATL;
list.addLastBOS;
list.addFirstLAX;
System.out.printlnlist;
System.out.printlnlistfirst;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started