lundi 27 juillet 2015

reverse linked list using recursion

I am trying to reverse a linked list using recursion.I made the reverse() function to reverse the list.I created a linked list in main() and also defined print() method. I don't know what mistake I am making.Please help me correct it.The code snippets are given below.

struct node
{
    int data;
    struct node *next;
}*head;

void reverse(node **firstnode,node *n)
{
    if(n==NULL)
    {
        head=n;
        return;
    }
    reverse(&head,n->next);
    struct node *q=n->next;
    n->next=q;
    q->next=NULL;
}

void main()
{
    ......

    head=first;
    reverse(&first,first);
    print(head);
}

Aucun commentaire:

Enregistrer un commentaire