Question: question about ava In the existing DoublyLinkedList class, write and test a non-static method named reverseList to reverse a doubly linked list. Write the testing

question about ava

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 linkedlists; public class DoublyLinkedList { private static class Node { private E element; private Node prev; private Node next; public Node(E 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 setPrev(Node p) { prev = p; } public void setNext(Node n) { next = n; } } private Node header; private Node trailer; private int size = 0; public DoublyLinkedList() { header = new Node<>(null, null, null); trailer = new Node<>(null, header, null); header.setNext(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(); } public E last() { if (isEmpty()) return null; return trailer.getPrev().getElement(); trailer } public void addFirst(E e) { addBetween(e, header, header.getNext()); } public void addLast(E e) { addBetween(e, trailer.getPrev(), trailer); } public E removeFirst() { if (isEmpty()) return null; return remove(header.getNext()); } public E removeLast() { if (isEmpty()) return null; return remove(trailer.getPrev()); } 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(); } //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()); // } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To implement the reverseList method for the DoublyLinkedList class and to test it in the main method you need to follow these steps Create the reverseList method This method will traverse the list and ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!