Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do I get the length of a node using recursion?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ilia G




PostPosted: Mon Jun 30, 2008 2:11 pm   Post subject: 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



Node.java
 Description:

Download
 Filename:  Node.java
 Filesize:  607 Bytes
 Downloaded:  89 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Vermette




PostPosted: Mon Jun 30, 2008 2:30 pm   Post subject: 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




PostPosted: Mon Jun 30, 2008 11:13 pm   Post subject: 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




PostPosted: Mon Jun 30, 2008 11:36 pm   Post subject: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: