Recursive function for singly linked list

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;
  }
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);
}
nevermind then lol
Last edited on
Topic archived. No new replies allowed.