Does this code do everything the question asks ? JAVA public int value(Node node
ID: 3806456 • Letter: D
Question
Does this code do everything the question asks ? JAVA
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
}
Explanation / Answer
Yes the code looks fine,working, try running it on your compiler first, and let me know if any issues.
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.