Simple Recursion

  • Thread starter craig delehanty
  • Start date
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
 
K

Karl Heinz Buchegger

void List::addOne(Lnode *t)
{
if(t->next == NULL)
t->val += 1;

Read it in plain english:

if and only if the value of the next field of the node where t points
to equals NULL, add 1 to t->val.

That fact that this adds 1 to the first node in your list (which clearly
has not a value of NULL for its next field) indicates for a problem in
the way you either:
* build up your list
* you output the list

What you want to do is:

void List:: addOne( Lnode* t )
{
if( t == NULL )
return;
addOne( t->next ); // recurse until the end of the list is reached

t->val += 1; // add 1 to the current node ...
} // ... and unwind the recursion one step
Once the pointer t->next == NULL should not the function add 1 to each
node as it reverses out of the list?

As you have written it now, no. 1 is added only if the t->next
equals NULL. That's what you have written and that's what the
computer does.
 
P

Peter van Merkerk

craig delehanty said:
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?

No, only the last node will be incremented because the increment will
only happen when t->next == NULL. When the last node is encountered, it
will do the increment, then it will return and nothing else will happen
because the else clause doesn't do anything after the addOne() call. If
you would have used a debugger it would be easy to see why this function
doesn't work the way you expected.

One way to fix that function is this:
void List::addOne(Lnode *t)
{
if(t->next != NULL)
{
addOne(t->next);
}
t->val += 1;
}

Though I wonder why you userecursion for this. The iterative version is
simpler, and more efficient and won't run out of stack space when the
linked list is really long.

void List::addOne(Lnode *t)
{
while(t)
{
t->val += 1;
t = t->next;
}
}
 
W

wogston

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.

I don't see why it should be recursive, do something like this:

while ( node )
{
increase(node);
node = node->next;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top