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

Mannu's Avatar
Banned
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); }
0
Mannu's Avatar
Banned
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 shabbir; 30Mar2011 at 23:17.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Should use a loop instead of tail-recursion, then it won't use as much stack space.