Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to compare two linked lists? Write a C program to compare two linked lists. (http://www.go4expert.com/forums/compare-linked-lists-write-c-program-t25373/)

Mannu 30Mar2011 13:17

How to compare two linked lists? Write a C program to compare two linked lists.
 
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); }

Mannu 30Mar2011 13:20

Re: How to compare two linked lists? Write a C program to compare two linked lists.
 
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);
 }


xpi0t0s 31Mar2011 19:33

Re: How to compare two linked lists? Write a C program to compare two linked lists.
 
Should use a loop instead of tail-recursion, then it won't use as much stack space.


All times are GMT +5.5. The time now is 07:35.