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:
    http://www.landlordssolutions.com/
    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:
    http://www.landlordssolutions.com/
    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,009
    Likes Received:
    203
    Trophy Points:
    63
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice