Friday, Jan 19th

Last update12:59:40 PM GMT

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

Write e-mail

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


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

Share this post



Web Hosting