473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple Recursion

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

Jul 19 '05 #1
4 2494


craig delehanty wrote:
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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

"craig delehanty" <cr*******@e3.net.nz> wrote in message
news:3f******@news.iconz.co.nz...
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;
}
}

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Craig

Jul 19 '05 #3


Peter van Merkerk wrote:


Though I wonder why you userecursion for this.


It could be a homework exercise to get used to
recursions.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4
> 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;
}
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: MetalOne | last post by:
I am trying to write a generator function that yields the index position of each set bit in a mask. e.g. for x in bitIndexGenerator(0x16): #10110 print x --> 1 2 4 This is what I have, but...
5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
0
by: Chris Thomasson | last post by:
Here is a "simple" method for using the preprocessor to generate code via. recursion... What do you think of my experimental recursion implementation? Can you compile it? Any comments are...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.