Saturday, February 23, 2013

REVERSE LINKS IN LINKED LIST

/ * 
  *Reverse a linked list
  * This program reverse the links in a linked list. but not the data.
  */

void reverse_link(NODE **root)
{
    NODE *p,*q,*r=NULL;

    if(*root == NULL)
        return;

    p=*root;

    if(p->next != NULL)
    {
        q=p->next;
        p->next = NULL;
        while(q!=NULL)
        {
            r=q->next;  /* First store the next link address */
            q->next=p; /*  Reverse the link */
            p=q;           /* Move forward */
            q=r;
        }
        *root = p;  /* root is at last node */
    }
    return;
}

No comments:

Post a Comment