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

Exercise 2
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)"
Code:
package ex2;
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 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);
}
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){
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 static void main(String[] args)
{
DoublyLinkedList list = new DoublyLinkedList();
list.addFirst("MSP");
list.addLast("ATL");
list.addLast("BOS");
list.addFirst("LAX");
System.out.println("Original list: "+ list.toString());
System.out.println("Reversed list: "+ list.toString());
System.out.println(list);
System.out.println(list.first());
}
}
 Exercise 2 In the existing DoublyLinkedList class, write and test a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!