Question: In java, the goal is to complete the remove and addbetween methods. These are retaining to doubly linked lists. Insturctions can be found in code
In java, the goal is to complete the "remove" and "addbetween" methods. These are retaining to doubly linked lists. Insturctions can be found in code provided
The output should be:
Test MyDoublyLinkedList:.....
public class MyDoublyLinkedList
Data field
public Node header; parent of head, dummy node
public Node trailer; child of tail, dummy node
public int size ;
Method field
public MyDoublyLinkedList
header new Nodenull null, null;
trailer new Nodenull header, null; header trailer
header.setNexttrailer; header trailer
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;
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
Complete this block
private E removeNode node
Complete this block
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;
public static void mainString args
System.out.printlnTest MyDoublyLinkedList:.....";
MyDoublyLinkedList list new MyDoublyLinkedList;
list.addLast;
list.addLast;
list.addLast;
list.addLast;
list.addLast;
System.out.printlnlisttoString;
list.addFirst;
list.addFirst;
System.out.printlnlisttoString;
list.addBetween list.header.getNext list.header.getNextgetNext;
System.out.printlnlisttoString;
list.removeFirst;
System.out.printlnlisttoString;
list.removeLast;
System.out.printlnlisttoString;
Node class
class Node
private E element;
private Node prev;
private Node next;
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;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
