I am requested to write a driver function to call a recursive function. I was wondering what i need to do in the driver function.
this program is to reverse a linked list.
void invert_r()
{
//This is the driver function for recursive_invert
nodeType<Type> *p, *q;
q = first;
p = first;
recursive_invert(q, p);
}
nodeType<Type>* recursive_invert(nodeType<Type> *q, nodeType<Type> *p)
{
//Invert a linked list using recursion.
//DO NOT create any new node in your implementation
if(p -> link == NULL)
{
q -> link = p;
return p;
}
else
{
recursive_invert(p -> link, q) -> link = p;
}
return p;
}
firstis. Otherwise everything looks fine. What specific problems do you get with this code?nodeType<Type> *p = first; nodeType<Type> *q = p->link;and probablyfirst = recursive_invert(p, q);, don't you? At the least, you should not casually discard the return value from the recursive function since it is now the head of the list (and if you lose it, you won't be able to get to the start of the list again).