
-----------------------------------
Ilia G
Mon Jun 30, 2008 2:11 pm

How do I get the length of a node using recursion?
-----------------------------------
Hi guys, I got a problem with the following method.
I tryed to do something with it, but had no luck, any help would be appreciated 

  public static int countNodes(Node head)
    // post: returns the number of nodes in a single linked list.
  {
   
    if(head.getItem()== null) 
    {
      return 0;
    }
    else 
    {
      int size = countNodes(head);
      return size;
    }
  }

Note: the file below contains all the methods that can be used on a Node

-----------------------------------
Vermette
Mon Jun 30, 2008 2:30 pm

RE:How do I get the length of a node using recursion?
-----------------------------------
Take a closer look at your recursive call.  I can't really speak plainer than that without outright giving you the answer.

-----------------------------------
Ilia G
Mon Jun 30, 2008 11:13 pm

Re: How do I get the length of a node using recursion?
-----------------------------------
The only problem is I don't really know how to do the recursive case in this method, or steps i should take to knowing which code i should use for it. I've tryed many times to get it to work, with no luck. If anyone could tell me how i can do the recursive case, i'd appreciate it

-----------------------------------
r691175002
Mon Jun 30, 2008 11:36 pm

Re: How do I get the length of a node using recursion?
-----------------------------------
You are returning 0 every time.  Work through what your function does in your head.  If you are counting something, you are going to need to increment something somewhere.
