1 2 Previous Next 17 Replies Latest reply: Oct 18, 2006 3:46 PM by 807598 RSS

    Help - recursion method

    807598
      Tried to get help in the java programming section, but didn't find any takers... Can anyone here help?

      Hello,

      My error is that I'm creating a method that returns an integer, but I can't seem to fix the problem. I'm missing something. Can anyone help? My method finds the maximum value in a linked list.
      public static int maxElement(Node head, int max){
                
                current = head;
                
                if(current == null){
                     System.out.println("No maximum value");
                     return max;
                }
                
                else if(current.link == null)
                     return max = current.info;
                
                else if(current.info > current.link.info){
                     max = current.info;
                     return (maxElement(current.link, max));
                }
                
                else if(current.info < current.link.info){
                     max = current.link.info;
                     return (maxElement(current.link, max));
                }
           }
        • 1. Re: Help - recursion method
          807598
          My error is that I'm creating a method that returns an integer, but I can't seem to fix the problem.
          well, that's easy. sort out whatever the problem is, and then the error will go away

          what I mean is, what actually is the error?
          • 2. Re: Help - recursion method
            807598
            With quick look this is incorrect:
            else if(current.link == null)
                           return max = current.info;
            should be
            else if(current.link == null)
                           return max;
            • 3. Re: Help - recursion method
              807598
              With quick look this is incorrect:
              else if(current.link == null)
                             return max = current.info;
              should be
              else if(current.link == null)
                             return max;
              I get the following error message: "This method must return a result of type int."

              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
                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
                  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? :)
                  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?
                  else if(current.link == null)
                                 return max = current.info;
                  How then, do I traverse to compare the next node with max? I thought that my next if statement did that...

                  Thanks for you help,
                  aiki
                  • 6. Re: Help - recursion method
                    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?
                    else if(current.link == null)
                                   return max = current.info;
                    How then, do I traverse to compare the next node with
                    max? I thought that my next if statement did
                    that...

                    Thanks for you help,
                    aiki
                    You need a base case...your first check.

                    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
                      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
                        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.
                        else if(current.link == null)
                                       return max = current.info;
                        This is not right?
                        • 9. Re: Help - recursion method
                          807598
                          System.out.print(maxElement(head,max));
                          Do not set "max" to predefined value. Set it to value
                          of first element.
                          Something like this:
                          else if(current.info > current.link.info){
                                         //max = current.info;  disregarded for now...
                                         return (maxElement(current.link, current.info));
                          Is this what you mean?
                          aiki
                          • 10. Re: Help - recursion method
                            807598
                            Did you read my answer?
                            • 11. Re: Help - recursion method
                              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.
                              else if(current.link == null)
                                             return max = current.info;
                              This is not right?
                              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.
                              • 12. Re: Help - recursion method
                                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
                                  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
                                    Then your base case should return max.

                                    And when you recurse you should pass max+1
                                    1 2 Previous Next