1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to compare two linked lists? Write a C program to compare two linked lists.

Discussion in 'C' started by Mannu, Mar 30, 2011.

  1. Mannu

    Mannu Banned

    Joined:
    Mar 3, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    SEO executive
    Location:
    New Delhi(india)
    Home Page:
    int compare_linked_lists(struct node *q, struct node *r) { static int flag; if((q==NULL ) && (r==NULL)) { flag=1; } else { if(q==NULL || r==NULL) { flag=0; } if(q->data!=r->data) { flag=0; } else { compare_linked_lists(q->link,r->link); } } return(flag); }
     
  2. Mannu

    Mannu Banned

    Joined:
    Mar 3, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    SEO executive
    Location:
    New Delhi(india)
    Home Page:
    Code:
    int compare_linked_lists(struct node *q, struct node *r) 
    {     
    static int flag;         if((q==NULL ) && (r==NULL))     
    {          
    flag=1;
     } 
        else
     {
     if(q==NULL || r==NULL)         
    {
     flag=0;
     }
     if(q->data!=r->data) 
            { 
                flag=0; 
            }         
    else 
            { 
               compare_linked_lists(q->link,r->link);
     } 
        }     
    return(flag);
     }
     
    Last edited by a moderator: Mar 30, 2011
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Should use a loop instead of tail-recursion, then it won't use as much stack space.
     

Share This Page