Home
Questions
Search
Forum
Contact
Guest Book
Polls!
Got a Question?
 
PREV
Linked Lists
NEXT
 
(10 / 301)
 
 



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


PREV
COMMENTS                                  INDEX                                  PRINT
NEXT



Last updated: November 3, 2005

www.cracktheinterview.com - Your destination for the most common IT interview questions, answers, frequently asked interview questions (FAQ), C Programs, C Datastructures for technical interviews conducted by the top IT companies around the world!