public int value(Node node) { //everything this right in your program just you h
ID: 3806437 • Letter: P
Question
public int value(Node node)
{
//everything this right in your program just you have to add number of links to the different node value and return it
int diffNode=0;
int linkscount=0;
while(node!=null)
{
if(node.data!=target.data)
{
diffNode++;
linkscount++;
}
node=node.prev; // going back to previous node
}
return diffNode+linkscount;
}
b. A value method value PLUS the number Node letters different number of and returns the of (node) takes a links back to startExplanation / Answer
Here is the corrected code, along with the detailed expmanation in the comments.
public int value(Node node)
{
int value=node.data; //Saves data in node of node, passed in the function
int diffNode=0; //to count no. of data different from the value
int linkscount=0; //to count total number of links
while(node!=null)
{
if(node.data!=value) //There would be a minor change here. SInce this function doesn't have "target" node, and hence, "target.data" will give error.
//So, we replace it with "value=node.data". When node changes after every loop, the data in "value" is same, used to compare data of every node.
{
diffNode++;
}
linkscount++; //THis increment operation would be outside the "if" condition
node=node.prev; // going back to previous node
}
return diffNode+linkscount; //returning the sum of diffNode and linkscount
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.