Thursday, May 24, 2007

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

62 comments:

Saranya said...

hey.. keep up d good work in dis blog :) by d way.. i ve a more compact recursive soln.. plz tell me if u find any bug..

int comp(struct node *p,struct node *q)
{
if(!p&&!q)
return 1;
if((!p&&q)||(!q&&p))
return 0;
return((p->info==q->info)&&(comp(p->link,q->link)));
}

Lol,
Me

Sandeep Patra said...

Hi,
Gud job dude.
I have a doubt that this function would work in all conditions.
check the else part
else
{
if(q==NULL || r==NULL)
{
flag=0;
}
if(q->data!=r->data)
{
flag=0;
}
when q is NULL but r is not NULL it will come to the above else part. then it will check the first if condition and make the flag 0
now it will come to the second if condition and try to access the q->data. here q is NULL and it is trying to accesses q->data so it will crash.

Please pardon me if i was wrong somewhere.

Thanks and Regards,
Sandy

udit said...

More Compact
int comp(struct node *p,struct node *q)
{
if(!p&&!q)
return 1;
if((!p)||(!q)) /*LOL*/
return 0;
return((p->info==q->info)&&(comp(p->link,q->link)));
}

Anonymous said...

Thanks a lot!!Good job!! It is a very good collection!!

Pramod said...

Bool Compare(Node *a, Node* b) {
If (a== NULL && b== NULL) {
Return 0;
} else if (a== NULL) {
return -1;
} else if (b == NULL) {
return 1;
}

while (a != NULL && b!= NULL) {
lastCompare = compare(a.data, b.data);
if (lastCompare != 0) {
return lastCompare;
}
A = a->next;
B = b->next;
}

if (a== NULL && b== NULL) {
Return lastCompare;
} else if (a== NULL) {
return -1;
} else if (b == NULL) {
return 1;
}
}

amir malik said...

if(q==NULL || r==NULL)
{
flag=0;
return(flag); -->[to avoid crash when
either q or r is NULL]
}
Please correct if i am wrong

Anonymous said...

xanax online xanax blue pill side effects - where to buy alprazolam powder

Anonymous said...

buy xanax 2mg cheap xanax no prescription online - not getting high xanax

Anonymous said...

buy tramadol online tramadol hcl 50 mg po tab - buy tramadol hydrochloride online usa

Anonymous said...

buy tramadol online tramadol 50 mg white pill - tramadol online no script

Anonymous said...

generic xanax xanax bars 15 - buy xanax generic online

Anonymous said...

buy tramadol online tramadol withdrawal long symptoms - can tramadol overdose fatal

Anonymous said...

generic xanax xanax side effects coming off - xanax xr 2 mg side effects

Anonymous said...

buy tramadol with cod tramadol dosage for people - buy tramadol 100mg

Anonymous said...

alprazolam drug will a .25 xanax show up in a drug test - xanax dosage depression

Anonymous said...

xanax mg buy xanax philippines - xanax dosage dosage

Anonymous said...

buy carisoprodol soma carisoprodol tablets side effects - carisoprodol urine test

Anonymous said...

buy tramadol online tramadol for dogs and people the same - generic name for tramadol

Anonymous said...

buy tramadol online buy tramadol 200mg online - tramadol 50mg side effects

Anonymous said...

cheap tadalafil cialis no effect - can i order cialis online

Anonymous said...

xanax online buy brand xanax online - xanax effects cortisol

Anonymous said...

buy cialis online should you buy cialis online - cialis online without rx

Anonymous said...

buy tramadol online tramadol extended release half life - buy tramadol no prescription usa

Anonymous said...

cialis online buy cialis online fast shipping generic - cialis 2.5 mg price

Anonymous said...

cialis online cialis online nz - cialis goes generic

Anonymous said...

xanax online xanax bars milligrams - xanax side effects paranoia

Anonymous said...

buy cialis online cialis online rezeptfrei kaufen - cialis online is it safe

Anonymous said...

cialis cost cialis online without - cialis 30 day free trial coupon

Anonymous said...

cialis online buy cialis online canadian pharmacy - cialis upset stomach

Anonymous said...

http://landvoicelearning.com/#51602 tramadol 627 - ultram 50 mg oral tablet

Anonymous said...

http://landvoicelearning.com/#57594 tramadol dosage schedule - buy tramadol in missouri

Anonymous said...

buy tramadol usa tramadol dosage nz - tramadol for dogs side effects

Anonymous said...

buy tramadol tramadol dosage for opiate withdrawal - buy tramadol usa

Anonymous said...

http://buytramadolonlinecool.com/#96430 tramadol youtube - effects of tramadol high

Anonymous said...

learn how to buy tramdadol buy tramadol online no prescription - generic tramadol online no prescription

Anonymous said...

07 prednisone asthma - prednisone 5mg http://www.prednisoneonlinerx.net/index.html, [url=http://www.prednisoneonlinerx.net/index.html]prednisone acne [/url]

Anonymous said...

buy tramadol tramadol dosage rat - buy tramadol visa

Anonymous said...

buy tramadol buy tramadol online by cod - tramadol hcl 225 mg

Anonymous said...

learn how to buy tramdadol tramadol online mastercard - tramadol 50 mg tab amne

Anonymous said...

buy tramadol tramadol generic cost - tramadol for dogs in usa

Anonymous said...

buy tramadol online tramadol for dogs expiration date - buy tramadol usa pharmacy

Anonymous said...

buy tramadol cod fedex buy tramadol usa - tramadol online no prescription overnight

Anonymous said...

buy tramadol buy tramadol online cheap - buy tramadol forum

Anonymous said...

buy tramadol online buy cheap tramadol online - can you buy tramadol over counter us

Anonymous said...

buy tramadol signs of tramadol addiction - countries can you buy tramadol over counter

Anonymous said...

http://reidmoody.com/#65473 lorazepam online no prescription - normal daily dosage ativan

Anonymous said...

buy ativan online side effects generic ativan - kicking ativan addiction

Anonymous said...

buy ativan online ativan withdrawal nightmares - ativan side effects adults

Anonymous said...

ativan online ativan and alcohol abuse - ativan dosage elderly

Anonymous said...

buy tramadol online tramadol medication - can get high tramadol 50mg

Anonymous said...

buy tramadol online buy tramadol legal - tramadol hcl vs norco

Anonymous said...

xanax 2mg do xanax side effects go away - xanax side effects day after

Anonymous said...

buy xanax online xanax orange pill - xanax bars to get high

Anonymous said...

buy xanax online xanax japan - xanax online no prescription paypal

Anonymous said...

buy xanax online no rx xanax bars 56 65 - sizes of xanax pills

Anonymous said...

how to buy xanax online legally xanax bars no writing - cheap xanax sale

Anonymous said...

xanax medication xanax vs generic brands - xanax bars how long in system

Anonymous said...

Wow! This blog looks just like my old one! It's on a totally different topic but it has pretty much the same page layout and design. Great choice of colors!

My web site - the tao of badass attraction system

Anonymous said...

After looking at a handful of the articles on your website, I honestly like your technique of writing a blog.
I bookmarked it to my bookmark site list and will be checking back soon.
Please visit my web site as well and let me know your opinion.



Take a look at my website :: tao badass ezgprodurl

Anonymous said...

Тhe write-up features verified useful to me.
It’s very eduсatіonal and уou really аre obviοusly quіtе
well-infoгmеd of this type. You pοssess
eхpoѕеd ouг eyes fог уou to varying viеws on this kіnd of matter uѕіng intгiquing, nοtable and reliable written сontent.


Visit my blog :: valium

Anonymous said...

I got this site from my buddy who shared with me
about this web page and at the moment this time I am visiting this web page and
reading very informative articles at this time.


Feel free to surf to my page ... website

Anonymous said...

http://www.achildsplace.org/banners/tramadolonline/#5962 buy tramadol online no prescription cod - buy tramadolcod fedex