Recursive function for singly linked list

Oct 18, 2017 at 5:54pm
I wrote a function to count the number of nodes in a singly linked list that should have 5 nodes, but my function is returning 1. What did I do wrong?

1
2
3
4
5
6
7
8
9
  int SLinkedList::GetCount(Node*temp){
	int count = 0;
	if(temp != NULL){
		count =  count + 1;
		temp = temp->link;
		GetCount(temp);
	}
	return count;
  }
Oct 18, 2017 at 6:09pm
nevermind, i figured it out. Here is the working function in case anybody needs it

1
2
3
4
5
6
int SLinkedList::GetCount(Node*temp){
	if(temp==NULL){
		return 0;
	}
	return 1 + GetCount(temp->link);
}
Oct 18, 2017 at 6:13pm
nevermind then lol
Last edited on Oct 18, 2017 at 6:25pm
Topic archived. No new replies allowed.