C
craig delehanty
I have a program that creates a linked list. I have added 3 nodes with
the following values.
head -> 4 -> 13 -> 19 -> NULL
Below is a simple recursive function.I want to advance through the list
until it finds NULL. Then as it unwinds I wish it to add 1 to each value
of each node. My problem is it only adds 1 to the 1st created node.
The function below gives me the following output
head -> 4 -> 13 -> 20 -> NULL, I want: head -> 5 -> 14 -> 20 -> NULL
void List::addOne(Lnode *t)
{
if(t->next == NULL)
t->val += 1;
else
addOne(t->next);
}
void List::addOne()
{
addOne(head);
}
Once the pointer t->next == NULL should not the function add 1 to each
node as it reverses out of the list?
Can anyone advise me of my error please?
Craig
the following values.
head -> 4 -> 13 -> 19 -> NULL
Below is a simple recursive function.I want to advance through the list
until it finds NULL. Then as it unwinds I wish it to add 1 to each value
of each node. My problem is it only adds 1 to the 1st created node.
The function below gives me the following output
head -> 4 -> 13 -> 20 -> NULL, I want: head -> 5 -> 14 -> 20 -> NULL
void List::addOne(Lnode *t)
{
if(t->next == NULL)
t->val += 1;
else
addOne(t->next);
}
void List::addOne()
{
addOne(head);
}
Once the pointer t->next == NULL should not the function add 1 to each
node as it reverses out of the list?
Can anyone advise me of my error please?
Craig