Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Suppose you have a doubly linked list in which the prev references have the correct values but the next references have not yet been initialized.
Suppose you have a doubly linked list in which the prev references have the correct values but the next references have not yet been initialized. Write a static method called initNexts that:
Takes one parameter, a reference to the last node of the linked list
Traverses the linked list from back to front, filling in the next references
Returns a reference to the first node in the linked list.
You may assume that there is at least one node in the list.
A class for representing a string using a doublylinked list.
Each character of the string is stored in a separate node.
This class represents one node of the linked list. The string as a
whole is represented by storing a reference to the first node in
the linked list. Empty strings are represented using a value of null.
public class DNode
private char ch;
private DNode prev;
private DNode next;
Constructor
public DNodechar c DNode p DNode n
this.ch c;
this.prev p;
this.next n;
convert converts a standard Java String object to a doublylinked
list and returns a reference to first node in that doublylinked list
public static DNode convertString s
if slength
return null;
DNode firstNode new DNodescharAt null, null;
DNode prevNode firstNode;
DNode nextNode;
for int i ; i slength; i
nextNode new DNodescharAti prevNode, null;
prevNode.next nextNode;
prevNode nextNode;
return firstNode;
removeNexts takes a reference to the first node in a
doublylinked list, sets all of the next fields in the nodes
to null, and returns a reference to the last node in the
linked list.
This method can be used to create a scenario like the one
envisioned for the initNexts method that you need to write for
Problem
public static DNode removeNextsDNode first
DNode trav first;
DNode trail null; will stay one behind trav
while trav null
the order of these statements matters!
trail trav;
trav trav.next;
trail.next null;
at the end of the loop, trail will be pointing
to the last node
return trail;
problem
public static DNode initNextsDNode last
add your implementation of the method here
toString creates and returns the Java string that
the current DNode represents. Note that this
method is nonstatic method, and thus it won't work
for empty strings, since they are represented by a
value of null, and we can't use null to invoke this method.
public String toString
String str ;
DNode trav this; start trav on the current node this
while trav null
str str trav.ch;
trav trav.next;
return str;
public static void mainString args
DNode str DNode.convertcrimson;
System.out.println
initial str: str;
DNode last DNode.removeNextsstr;
System.out.printlnafter setting next fields to null: str;
str DNode.initNextslast;
System.out.printlnafter calling initNexts: str;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started