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

October 28, 2007 · Filed Under Linked List, Placement Questions 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

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).

Related Articles


Get Latest JobsAdda.com Jobs, news & updates  via Email