1

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;
}
8
  • Hard to say as long we can't see what first is. Otherwise everything looks fine. What specific problems do you get with this code? Commented Oct 22, 2013 at 14:21
  • @g-makulik "first" is the first element of the linked list. i am confused about the use of driver function and how to use it and then call my recursive function... Commented Oct 22, 2013 at 14:22
  • the output of the function is that firstly reverse the linked list by iterative method. and then convert back the linked list by implementing the invert_r() to call the recursive one Commented Oct 22, 2013 at 14:24
  • If first is the first element of the linked list I think they have to be members of nodeType, so initialization on invert_r function should be made like this: q = q->first; Commented Oct 22, 2013 at 14:33
  • 1
    Since you need to pass (a pointer to) one node and (a pointer to) the next node, you need nodeType<Type> *p = first; nodeType<Type> *q = p->link; and probably first = 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). Commented Oct 22, 2013 at 16:42

1 Answer 1

1
void recursiveReverse(struct node** head_ref)
{
    struct node* first;
    struct node* rest;

    /* empty list */
    if (*head_ref == NULL)
       return;   

    /* suppose first = {1, 2, 3}, rest = {2, 3} */
    first = *head_ref;  
    rest  = first->next;

    /* List has only one node */
    if (rest == NULL)
       return;   

    /* reverse the rest list and put the first element at the end */
    recursiveReverse(&rest);
    first->next->next  = first;  

    /* tricky step -- see the diagram */
    first->next  = NULL;          

    /* fix the head pointer */
    *head_ref = rest;              
}
Sign up to request clarification or add additional context in comments.

4 Comments

Your code doesn't look anything like the OP's code. It's got a different signature and different structure members.
John it is only for illustration purpose as like an Algo.
Please note that we expect answers to address the specific issues in the question. If you have a very relevant alternative, you can consider adding that - but please be sure to address the question and explain exactly why, first.
Some explanation would be nice, but if you'd add that I think it might be a good answer ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.