This content has been marked as final.
Show 17 replies

1. Re: Help  recursion method
807598 Oct 18, 2006 11:40 AM (in response to 807598)
well, that's easy. sort out whatever the problem is, and then the error will go awayMy error is that I'm creating a method that returns an integer, but I can't seem to fix the problem.
what I mean is, what actually is the error? 
2. Re: Help  recursion method
807598 Oct 18, 2006 11:47 AM (in response to 807598)With quick look this is incorrect:
should beelse if(current.link == null) return max = current.info;
else if(current.link == null) return max;

3. Re: Help  recursion method
807598 Oct 18, 2006 11:57 AM (in response to 807598)With quick look this is incorrect:
I get the following error message: "This method must return a result of type int."
should beelse if(current.link == null) return max = current.info;
else if(current.link == null) return max;
The first if statement determines if the linked list contains any nodes at all. If there are no nodes to check, the method returns max value = 0;
The second if statement determines if the linked list only contains ONE node. In that case, we return the value of that node and assign it to max. I then return max.
Here is where I think my problem is, my third and fourth if statements. I'm now comparing the first node value with the second node value...instead of returning a maximum value, I want to call the method again to compare the next two nodes...etc.
In my main method, I'm calling the method like this: System.out.print(maxElement(head,max)); head is defined as the linked list and max has been initialized to zero. 
4. Re: Help  recursion method
807598 Oct 18, 2006 12:05 PM (in response to 807598)The second if statement determines if the linked list only contains ONE node. In that case, we return the value of that node and assign it to max. I then return max.
I understand propose of this statement but you don't understand recursion :)
Doesn't matter how many elements are in your list  then recursion walk to your last element your function will accept only this last element and treat it as ONE element list. Wrong way  yes? :) 
5. Re: Help  recursion method
807598 Oct 18, 2006 12:11 PM (in response to 807598)
o.k...then I am having trouble with the recursive portion of this method. Are you saying then that the second if statement, below, should only return max? Which at this point in my code is zero?The second if statement determines if the linked
list only contains ONE node. In that case, we return
the value of that node and assign it to max. I then
return max.
I understand propose of this statement but you don't
understand recursion :)
Doesn't matter how many elements are in your list 
then recursion walk to your last element your
function will accept only this last element and treat
it as ONE element list. Wrong way  yes? :)
How then, do I traverse to compare the next node with max? I thought that my next if statement did that...else if(current.link == null) return max = current.info;
Thanks for you help,
aiki 
6. Re: Help  recursion method
807598 Oct 18, 2006 12:13 PM (in response to 807598)o.k...then I am having trouble with the recursive
You need a base case...your first check.
portion of this method. Are you saying then that the
second if statement, below, should only return max?
Which at this point in my code is zero?
How then, do I traverse to compare the next node withelse if(current.link == null) return max = current.info;
max? I thought that my next if statement did
that...
Thanks for you help,
aiki
If the next node is null you're at the end...you return max.
next you recuse onto the next node.
Finally you just return the max value you already have. 
7. Re: Help  recursion method
807598 Oct 18, 2006 12:15 PM (in response to 807598)System.out.print(maxElement(head,max));
Do not set "max" to predefined value. Set it to value of first element. 
8. Re: Help  recursion method
807598 Oct 18, 2006 12:18 PM (in response to 807598)This is what I had considered my base case. This if statement checks to see if there is only one node (current.info is the value of the node and current.link is the address pointing to the next node. If current.link is null, then I only have one node. Therefore, my max value is current.info.
This is not right?else if(current.link == null) return max = current.info;

9. Re: Help  recursion method
807598 Oct 18, 2006 12:21 PM (in response to 807598)
Something like this:System.out.print(maxElement(head,max));
Do not set "max" to predefined value. Set it to value
of first element.
Is this what you mean?else if(current.info > current.link.info){ //max = current.info; disregarded for now... return (maxElement(current.link, current.info));
aiki 
10. Re: Help  recursion method
807598 Oct 18, 2006 12:22 PM (in response to 807598)Did you read my answer? 
11. Re: Help  recursion method
807598 Oct 18, 2006 12:22 PM (in response to 807598)This is what I had considered my base case. This if
Good luck working this out. I'm not even sure what you're recursing over, but if I had to go out on a limb I'd say No, this is not right.
statement checks to see if there is only one node
(current.info is the value of the node and
current.link is the address pointing to the next
node. If current.link is null, then I only have one
node. Therefore, my max value is current.info.
This is not right?else if(current.link == null) return max = current.info;

12. Re: Help  recursion method
807598 Oct 18, 2006 12:27 PM (in response to 807598)Is this what you mean?
No this is not.
Set max before calling your function first time. OUTSIDE of your function. Immediately BEFORE line with call to System.out.println(). 
13. Re: Help  recursion method
807598 Oct 18, 2006 12:32 PM (in response to 807598)Before calling my method, I have max initialized to zero.
I then call my functions with System.out.print(maxElement(head,max);
aiki 
14. Re: Help  recursion method
807598 Oct 18, 2006 12:47 PM (in response to 807598)Then your base case should return max.
And when you recurse you should pass max+1