Recursive function to display the number of nodes of a singly connected list?
-
Could someone give me an algorithm of a recursive function to display-NOT TO RETURN-the number of nodes of a singly connected list?
I am probably misunderstanding your question, but any implementation of a List (ArrayList, LinkedList etc) should expose a Count property, and so your question of how to display this is down to where you want to display it. on the console: Console.WriteLine(myList.Count); in a messageBox MessageBox.Show(miList.Count.ToString()); sorry if ive misunderstood ;)
-
I am probably misunderstanding your question, but any implementation of a List (ArrayList, LinkedList etc) should expose a Count property, and so your question of how to display this is down to where you want to display it. on the console: Console.WriteLine(myList.Count); in a messageBox MessageBox.Show(miList.Count.ToString()); sorry if ive misunderstood ;)
-
while I Agree with wjousts assertion that a) this is not a particularly difficult question and b) you should do your own homework ;) - I will provide some assistance. You must know what a linked list is, if not go back and refer to your course notes. Recursion you probably also know about - essentially calling a function from itself (or put another way "to understand recursion, you must first understand recursion /geekhumour) So, to help write this algorithm, I find it easy to start off in pseudo-code
while {not at end of list)
Incrememnt counter
loop
return counterNow, I know you super-highlight-stressed that you want it DISPLAYED and not RETURNED!!11 (your homework said display it right?) however in order to dispaly something...it must first be returned from somewhere. Turning the above into a "recursive function" is the bit you've got to do. If this is not enough to get you going on your homework, then seriously consider whether you should be studying a subject you cannot do! Good luck.
-
Perhaps you should do you own homework? You learn a lot more that way. This isn't a difficult question.
-
What have you tried? Show people that you are thinking about this and not just too lazy to do your homework and you'll find people are much more responsive. You need to write a recursive function, something like this (you fill in the blanks):
public int CountNodes(Node n) { if(ExitCondition) // what's your exit condition? { return ?; // what should you return for your base case? } else { // what do you need to return here. What are you going to pass to the recursive call? return ? + CountNodes(?); } }
-
while I Agree with wjousts assertion that a) this is not a particularly difficult question and b) you should do your own homework ;) - I will provide some assistance. You must know what a linked list is, if not go back and refer to your course notes. Recursion you probably also know about - essentially calling a function from itself (or put another way "to understand recursion, you must first understand recursion /geekhumour) So, to help write this algorithm, I find it easy to start off in pseudo-code
while {not at end of list)
Incrememnt counter
loop
return counterNow, I know you super-highlight-stressed that you want it DISPLAYED and not RETURNED!!11 (your homework said display it right?) however in order to dispaly something...it must first be returned from somewhere. Turning the above into a "recursive function" is the bit you've got to do. If this is not enough to get you going on your homework, then seriously consider whether you should be studying a subject you cannot do! Good luck.
-
My English is not good. But I mean something like that:
int count(POINTER head) { return head!=NULL ? count(head->next)+1 : 0 ; }
But not to return the value. I WANT TO DISPLAY THE VALUE.(and this is difficult) -
Console.WriteLine(myList.Count(myList.FirstNode)) seriously, you couldnt work this out? Swap to a different course while you still can.