public class Node {
double content;
Node next;
Node(final double node_value)
{
content = node_value;
next = null;
}
Node(final double node_value, final Node next_node)
{
content = node_value;
next = next_node;
}
}
Node Class
public int delete(final int index)
{
Node iter = head;
for(int i=0; i<index; i )
iter = iter.next;
int value = iter.next.content;
iter.next = iter.next.next;
return value;
}
Delete method within list class
I cannot figure out how I can return the value of this. When I change value to double from int, it cannot return. If I change double content to int, then the rest of the program does not work.
Is there an alternative to how I can finish this delete method?
CodePudding user response:
I guess you are using final keyword, It can only be initialised once.
So you wont be able to change the Node pointer after you initialised it once.
Remove final keyword from Node, I think it will work again.
Same is with the data type.
CodePudding user response:
The node's content is a double, so you should be accessing it via a double variable rather than the int variable. This will allow you to return the double value once you change the return type of your method to also be a double.
This should fix the issue:
public double delete(final int index)
{
Node iter = head;
for(int i=0; i<index; i )
iter = iter.next;
double value = iter.next.content;
iter.next = iter.next.next;
return value ;
}
int value
in the statement: int value = iter.next.content
doesn't make much sense when iter.next.content
is a going to be a double
P.S. Some notes:
- In java, there are certain semantics and syntax that should be followed. I strongly suggest you look into them to make your code more readable and easier to debug etc.. I added a tab under the for-loop.
- You may be accessing/deleting the incorrect node. Worth double checking!
Node iter = head
: head is not pointing to anything. It isn't passed as a method argument and it is not a class variable in the class code you provided- I'm almost certain this doesn't compile..