Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 1st, 2013, 04:04 AM
 sergerde
Registered User

Join Date: Jan 2013
Posts: 1
Time spent in forums: 26 m 21 sec
Reputation Power: 0
How to find the even numbers in a given linked list by using recursion?

I could do better things in the first day of the year but i'm trying to implement the linked lists and recursion together.

I just thought that how I can achieve to write a function that calculates the even numbers in a linked list with recursion.

This is a draft of the recursion code i know this is wrong but how should I implement the code?

void List:: findingEvens(Node* n){
if(n == NULL)
return;
else
if(n-> data % 2 != 0)
findingEvens(n-> next);
else
findingEvents(n-> next);
}

#2
January 1st, 2013, 11:19 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,213
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 4 m 30 sec
Reputation Power: 455
Recursively traversing a singly linked list is not sane.

Misspelling evens as events doesn't look useful.

if (condition) same_action;
else same_action;

is pathetic.

Happy new year. Mine's not starting happy, and I'm passing it along. Sorry.
__________________
[code]Code tags[/code] are essential for python code!

#3
January 2nd, 2013, 03:23 PM
Contributing User

Join Date: Aug 2009
Posts: 149
Time spent in forums: 3 Days 12 h 33 m 32 sec
Reputation Power: 36

Code:
```int findingEvens(node L, int E)

if(null L)
return 0;

if(L->element is even)
if(null L->next)
return E + 1
else
return findingEvens(L->next, E + 1)
else
if(null L->next)
return E
else
return findingEvens(L->next, E)```

 Viewing: Dev Shed Forums > Programming Languages > C Programming > How to find the even numbers in a given linked list by using recursion?