Answered step by step
Verified Expert Solution
Link Copied!

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 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:
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 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 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();
}
public void addFirst(E e){
addBetween(e, header, header.getNext());
}
public void addLast(E e){
addBetween(e, trailer.getPrev(), trailer); // place just before the trailer
}
public E removeFirst(){
if (isEmpty()) return null;
return remove(header.getNext());
}
public E removeLast(){
if (isEmpty()) return null;
return remove(trailer.getPrev());
}
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();
}
//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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 1 Lnai 9851

Authors: Paolo Frasconi ,Niels Landwehr ,Giuseppe Manco ,Jilles Vreeken

1st Edition

3319461273, 978-3319461274

More Books

Students also viewed these Databases questions