Hello everyone,
I am trying to implement a function that goes through a doubly linked list of integer values and adds the values recursively.
My class consists of a Node class that has the private data set with right and left pointers and the value. The public section consists of getters for the value and the left and right pointers.
Now Assuming that the list is already populated. This is the function that I have implemented so far to recursively add the values from the list but I am not sure if this is a proper implementation of recursion?
I am trying to add the value of the current node with the sum of the rest of the list. Any hints of suggestion is very much appreciated. Thanks in advance for your help!
Code:
int sum(Node* list)
{
if (list==NULL) return 0;
else
{
while (list!=NULL)
return list->getValue()+sum(list->getRight());
}
}