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 main method of the class DoublyLinkedList. For this purpose, you must only use and update the DoublyLinkedList.java file provided in Lesson2Examples posted in the eCentennial module “Lesson Examples (from textbook)”     package Ex2; public class DoublyLinkedList { private static class Node { private E element; // reference to the element stored at this node private Node prev; // reference to the previous node in the list private Node next; // reference to the subsequent node in the list public Node(E e, Node p, Node n) { element = e; prev = p; next = n; } // public accessor methods public E getElement() { return element; } public Node getPrev() { return prev; } public Node getNext() { return next; } // Update methods public void setPrev(Node p) { prev = p; } public void setNext(Node n) { next = n; } } //----------- end of nested Node class ----------- // instance variables of the DoublyLinkedList private Node header; // header sentinel private Node trailer; // trailer sentinel private int size = 0; // number of elements in the list public DoublyLinkedList() { header = new Node<>(null, null, null); // create header trailer = new Node<>(null, header, null); // trailer is preceded by header header.setNext(trailer); // header is followed by trailer } // public accessor methods public int size() { return size; } public boolean isEmpty() { return size == 0; } public E first() { if (isEmpty()) return null; return header.getNext().getElement(); // first element is beyond header } public E last() { if (isEmpty()) return null; return trailer.getPrev().getElement(); // last element is before trailer } // public update methods public void addFirst(E e) { addBetween(e, header, header.getNext()); // place just after the header } public void addLast(E e) { addBetween(e, trailer.getPrev(), trailer); // place just before the trailer } public E removeFirst() { if (isEmpty()) return null; // nothing to remove return remove(header.getNext()); // first element is beyond header } public E removeLast() { if (isEmpty()) return null; // nothing to remove return remove(trailer.getPrev()); // last element is before trailer } // private update methods private void addBetween(E e, Node predecessor, Node successor) { // create and link a new node Node newest = new Node<>(e, predecessor, successor); predecessor.setNext(newest); successor.setPrev(newest); size++; } private E remove(Node node) { Node predecessor = node.getPrev(); Node successor = node.getNext(); predecessor.setNext(successor); successor.setPrev(predecessor); size--; return node.getElement(); } public String toString() { StringBuilder sb = new StringBuilder("("); Node walk = header.getNext(); while (walk != trailer) { sb.append(walk.getElement()); walk = walk.getNext(); if (walk != trailer) sb.append(", "); } sb.append(")"); return sb.toString(); } public void reverseLIst() { if } //main method public static void main(String[] args) { //create and populate a doubly linked list DoublyLinkedList list = new DoublyLinkedList(); list.addFirst("MSP"); list.addLast("ATL"); list.addLast("BOS"); // list.addFirst("LAX"); System.out.println(list); System.out.println(list.first()); // } } //----------- end of DoublyLinkedList class -----------

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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 main method of the class DoublyLinkedList. For this purpose, you must only use and update the DoublyLinkedList.java file provided in Lesson2Examples posted in the eCentennial module “Lesson Examples (from textbook)”

 

 

package Ex2;


public class DoublyLinkedList<E> {

private static class Node<E> {

private E element; // reference to the element stored at this node

private Node<E> prev; // reference to the previous node in the list

private Node<E> next; // reference to the subsequent node in the list

public Node(E e, Node<E> p, Node<E> n) {
element = e;
prev = p;
next = n;
}

// public accessor methods
public E getElement() { return element; }

public Node<E> getPrev() { return prev; }

public Node<E> getNext() { return next; }

// Update methods
public void setPrev(Node<E> p) { prev = p; }

public void setNext(Node<E> n) { next = n; }

} //----------- end of nested Node class -----------

// instance variables of the DoublyLinkedList
private Node<E> header; // header sentinel

private Node<E> trailer; // trailer sentinel

private int size = 0; // number of elements in the list

public DoublyLinkedList() {
header = new Node<>(null, null, null); // create header
trailer = new Node<>(null, header, null); // trailer is preceded by header
header.setNext(trailer); // header is followed by trailer
}

// public accessor methods
public int size() { return size; }

public boolean isEmpty() { return size == 0; }

public E first() {
if (isEmpty()) return null;
return header.getNext().getElement(); // first element is beyond header
}

public E last() {
if (isEmpty()) return null;
return trailer.getPrev().getElement(); // last element is before trailer
}

// public update methods
public void addFirst(E e) {
addBetween(e, header, header.getNext()); // place just after the header
}

public void addLast(E e) {
addBetween(e, trailer.getPrev(), trailer); // place just before the trailer
}

public E removeFirst() {
if (isEmpty()) return null; // nothing to remove
return remove(header.getNext()); // first element is beyond header
}

public E removeLast() {
if (isEmpty()) return null; // nothing to remove
return remove(trailer.getPrev()); // last element is before trailer
}

// private update methods
private void addBetween(E e, Node<E> predecessor, Node<E> successor) {
// create and link a new node
Node<E> newest = new Node<>(e, predecessor, successor);
predecessor.setNext(newest);
successor.setPrev(newest);
size++;
}

private E remove(Node<E> node) {
Node<E> predecessor = node.getPrev();
Node<E> successor = node.getNext();
predecessor.setNext(successor);
successor.setPrev(predecessor);
size--;
return node.getElement();
}

public String toString() {
StringBuilder sb = new StringBuilder("(");
Node<E> walk = header.getNext();
while (walk != trailer) {
sb.append(walk.getElement());
walk = walk.getNext();
if (walk != trailer)
sb.append(", ");
}
sb.append(")");
return sb.toString();
}

public void reverseLIst() {
if
}


//main method
public static void main(String[] args)
{
//create and populate a doubly linked list
DoublyLinkedList<String> list = new DoublyLinkedList<String>();
list.addFirst("MSP");
list.addLast("ATL");
list.addLast("BOS");
//
list.addFirst("LAX");

System.out.println(list);
System.out.println(list.first());
//
}
} //----------- end of DoublyLinkedList class -----------

Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY