Sunday, 15 August 2010

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

Here is a simple C program to accomplish the same.


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);
}



Another way is to do it on similar lines as strcmp() compares two strings, character by character (here each node is like a character).

No comments:

Post a Comment