Question: Question Please write the code in the following methods: 1) reverseListv1() THIS IS THE ONE I NEED HELP BECAUSE I CANT FIGURE OUT A SIMPLER
Question
Please write the code in the following methods:
1) reverseListv1() THIS IS THE ONE I NEED HELP BECAUSE I CANT FIGURE OUT A SIMPLER WAY TO DO IT
public class DLList {
public DLLNode front;
public DLLNode back;
public DLList(String name) {
this.front = new DLLNode();
this.front.name = name;
this.front.nextnode = null;
this.front.prevnode = null;
this.back = front;
}
public void addNode(DLLNode newNode) {
newNode.nextnode = this.front;
this.front.prevnode = newNode;
this.front = newNode;
}
// 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 get a simpler version of a method to reverse a linked list.
public void reverseListv1() {
// 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. create a cursor node and point it to front in the original list.
// 4. create 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.
(According to the above requirement, type the code here)
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 = "";
(Type you code here)
}
}
public class DLLNode {
public DLLNode nextnode;
public DLLNode prevnode;
public String name;
public DLLNode() {
this.nextnode = null;
this.prevnode = null;
this.name = null;
}
public DLLNode(String name) {
this.nextnode = null;
this.prevnode = null;
this.name = name;
}
public DLLNode(String name, DLLNode nextnode, DLLNode prevnode) {
this.nextnode = nextnode;
this.prevnode = prevnode;
this.name = name;
}
public String nodeString() {
String result = "";
if (this.nextnode == null && this.prevnode == null) {
result = result + "front<--[- | " + this.name + " | -]-->back";
}
else {
result = result + "[- | " + this.name + " | -]";
}
return result;
}
}
public class DLLDriver {
public static void main(String[] args) {
DLList myList = new DLList("Isaac");
DLLNode friend1 = new DLLNode("Mikey");
DLLNode friend2 = new DLLNode("Julia");
myList.addNode(friend1);
myList.addNode(friend2);
myList.addNode(new DLLNode("Tim"));
myList.addNode(new DLLNode("Jon"));
// Testing reverse method 1
System.out.println(myList.toString());
myList.reverseListv1();
System.out.println(myList.toString());
System.out.println();
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
