I am having trouble writing these recursive functions. I know I am on the right track, but also know that these functions are not right. Any help would be appreciated.

Given the following declarations for a linked list:

``````struct NodeType;
struct NodeType {
int info;
};

typedef NodeType* NodePtr;``````

2.1. Write a recursive value-returning function Search that searches a dynamic linked list for the integer value key. If the value is in the list, the function should return a pointer to the node where it was found. If the value is not in the list, the function should return NULL.

``````NodePtr Search(NodePtr head, int key)  {
...
}``````

2.2. Write a recursive value-returning function FindMax that returns the maximum value in a dynamic linked list of integers.

``````int FindMax(NodePtr head) {
...
}``````

this is what I have so far
for 2.1

``````NodePtr Search( NodePtr head, key)
{
NodePtr current;

if(current==NULL)
{
return NULL;
}

while(current!=NULL)
{
if(current->info==key)
{
return current->info;
}

while(current->info!=key)
{
}

}``````

for 2.2

``````int FindMax (NodePtr head)
{
NodePtr current;

int value;

if(current==NULL)
{
return NULL;
}

while(current!=NULL)
{
value=current->info;

else

}
}``````
4
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by siddhant3s

>2.1. Write a recursive value-returning function that searches a linked list
>2.2. Write a recursive value-returning function that returns the maximum value in a linked list
"Recursive" and "linked list" are generally not terms that should be used in the same sentence. A linked list is normally a linear structure, and recursion isn't well suited to anything linear because there's a strong risk of stack overflow. When you use recursion, each new recursive call should simplify the problem approximately by half.

Besides, the non-recursive code is just as trivial for a linked list.

I know that's not helpful to your immediate problem, but I'm a bit pressed for time at the moment, so you'll have to wait until I'm more free.

Couple o' things:

Search has to return a pointer, so don't return info, return the pointer of the node you're looking at.

The recursive call of Search() has to take the same parameters as the first call, so don't forget to pass key.

That while loop at the end of search is trouble: if the condition wasn't true before calling Search(), it
still won't be true after you call search -- infinite loop. A simple if would do -- the recursion is taking the place of looping.

In FindMax, it would probably be helpful to pass the biggest value found so far as an input parameter.

thx

You have not written a recursive function.
Loosely speaking, Recursive functions should imply removal of ugly loops. I shall rather guide you in your first function.
You are using while to check an condition rather than if. This is unlikely (and maybe wrong too) by your instructor.
Here is a quick TODO for the first function:

``````START
* Check if Head is NULL; if it is return some value(preferably  zero) indicating failure of search
END``````

All the checks are to be made be if's and not while. The idea is not to use loops.

thx

Are these correct?

``````NodePtr Search( NodePtr head, key)
{

{
return 0 ;
}

{
else
}
}``````

Does the above code answer "If the value is not in the list, the function should return NULL."

``````int FindMax (NodePtr head)
{
int value;

{
return 0;
}

{

else

}
}``````

I know to the guy that posted above me not to use while statements but that is how she showed us, so I do not want change things around. Thank you for all who helped so far:)

Listen Kid, Recursion = avoiding loop.
I don't know why your instructor told you to use loop in a recursive function.
the while loop your using is doing all the trouble. It is making your function a junk.
Your code (for Search) is absolutely fine if you remove the while loop:

``````//corrected version. Loop removed
{