Question: Please make linked list box and arrow diagrams to explain how the addNode(DLLNode newNode) and toString() methods in the DLList.java class work. DLList.java: // A
- Please make linked list box and arrow diagrams to explain how the addNode(DLLNode newNode) and toString() methods in the DLList.java class work.
DLList.java:
// A DOUBLY-LINKED LIST CLASS
public class DLList {
public DLLNode front;
public DLLNode back;
// Constructor
public DLList(String name) {
this.front = new DLLNode();
this.front.name = name;
this.front.nextnode = null;
this.front.prevnode = null;
this.back = front;
}
// Add a node -- DON'T WORRY ABOUT KEEPING NODES IN SORTED ORDER!
// Just add them in any order to the front of the list.
public void addNode(DLLNode newNode) {
// Add new node in front of myDLfront.
// newNode.nextnode should be myDLfront
// What should myDLfront's previous and next nodes be?
newNode.nextnode = this.front;
this.front.prevnode = newNode;
this.front = newNode;
}
// Here is one way to reverse a linked list.
// This is pretty advanced and if you read this code,
// it is not immediately clear what is happening.
// YOU CAN SKIP THIS METHOD!
public void reverseListv1() {
// Buffer 1
DLLNode current = this.back; //set iterator to the back node
// Buffer 2
DLLNode prevBuffer = this.back.nextnode; //record (old) nextnode
DLLNode prev;
this.front = this.back; // set the list's "back" as the "front"
// This loop essentially travels backwards through the list,
// flipping "next" and "previous" pointers. You do it by setting
// current's next node and previous's previous node, using a saved
// record of the value.
while (current != null) {
current.nextnode = current.prevnode; // set current node's
nextnode as its previous
prev = current; //record last node processed
current = current.prevnode; //move iterator
prev.prevnode = prevBuffer; //set last processed node's prevnode
to its (old) nextnode
prevBuffer = current; //record last processed node's (old)
nextnode
this.back = prev; // set the list's "back" point to the last
processed node
}
}
// Think of the people waiting in the line. The last person in the example is
Isaac.
// Initially, his next person is nobody. When the list gets reversed, now you
need to
// tell Isacc that is next person is Mikey. Then you
// move on to Mikey. Mikey learns that his next person is Julia and in the
same
// loop iteration Isaac learns from the buffer that his previous person is
now nobody.
// The buffer then stores Isaac as the last person processed. When Julia
learns that
// her next person is Tim, in the same loop iteration, Mikey learns that his
previous
// person is now Isaac. They continue in this way until the old front of the
list is
// reached. This becomes the new end of the list, and the list reversal is
finished.
// Now a simpler version of a method to reverse a linked list.
public void reverseListv2() {
// The add method for this class adds items to the FRONT.
// So, iterating through from the first to the last node and
// adding each node in turn to our new list will end up
// reversing the list.
// 1. Get the name from the first node in the list.
// Use the name string to
// construct a new node in a new list "reversedList".
// 3. Make a cursor node and point it to front in the original list.
// 4. Make a while loop checking foe nextnode != null.
// 5. Iterate through the original list, using the names in the nodes
// to create new nodes that you add to the reversedList.
// 6. Outside the loop, set this.front to the new list's front.
String firstName = front.name;
//System.out.println(firstName);
DLList reversedList = new DLList(firstName);
//System.out.println(reversedList.toString());
DLLNode frontCursor = new DLLNode();
frontCursor = this.front;
while(frontCursor.nextnode != null) {
frontCursor = frontCursor.nextnode;
String currName = frontCursor.name;
DLLNode currNode = new DLLNode(currName);
reversedList.addNode(currNode);
}
this.front = reversedList.front;
}
public String toString(){
String toReturn = "front-->";
DLLNode currentF = this.front;
while(currentF!= null) {
toReturn = toReturn + currentF.nodeString() + "-->" ;
currentF = currentF.nextnode;
}
toReturn = toReturn + "back";
return toReturn;
}
// COMPLETE THE FOLLOWING METHOD THAT RATHER THAN
// REVERSING THE LIST, JUST PRODUCES A STRING THAT
// ALLOWS THE LIST TO BE PRINTED IN REVERSE ORDER.
public String reverseListString() {
String toReturn = "";
// YOUR CODE HERE.
return toReturn;
}
}


back"; } else { result = result + "[-] " + this.name + | -]"; } return result; == 11 11 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
