Monday, July 9, 2007

Write a program to add two long positive numbers (each represented by linked lists).

Check out this simple implementation


mynode *long_add(mynode *h1, mynode *h2, mynode *h3)
{
mynode *c, *c1, *c2;
int sum, carry, digit;

carry = 0;
c1 = h1->next;
c2 = h2->next;

while(c1 != h1 && c2 != h2)
{
sum = c1->value + c2->value + carry;
digit = sum % 10;
carry = sum / 10;

h3 = insertNode(digit, h3);

c1 = c1->next;
c2 = c2->next;
}

if(c1 != h1)
{
c = c1;
h = h1;
}
else
{
c = c2;
h = h2;
}

while(c != h)
{
sum = c->value + carry;
digit = sum % 10;
carry = sum / 10;
h3 = insertNode(digit, h3);
c = c->next;
}

if(carry==1)
{
h3 = insertNode(carry, h3);
}

return(h3);
}

11 comments:

Kevin said...

I may be missing something but how will the first loop ever terminate since h1 and h2 never change and c1 and c2 will hit the end of their lists.

Anonymous said...

Mysterious operations in the program.
Unable to understand the logic - please throw more light.

subha said...

the code is not understandable..is this code for a circularly linked list??please provide some explanation..

shitu said...

please provide some explenation

Unknown said...

Thanks for the info. It is very helpful.
RegardsEducational site
Get jobs info at Educational site

Anonymous said...

I will гight away snatch your rss feeԁ as
I сan not to find your e-mail subѕсription hyperlink or
e-newslettеr servісe. Do you have anу?

Pleasе permit me realize so that I could subscribe.
Thanks.

My hοmeρage; acne home remedy

Anonymous said...

WOW just what Ӏ waѕ loоking foг.
Came herе bу seаrсhing foг edωarԁϳonеs.
com\/benеfits

My wеb-sіte ... http://www.iamsport.org/pg/blog/HuntzingerFeiert/read/9958564/hmorrhoiden-nach-geburt-symptome

Anonymous said...

I was recommenԁed this webѕite by wаy оf my cοusin.
I am not ρositivе whether this put up is written by wаy of him as no one еlse realizе suсh detаiled about my diffіculty.
You're incredible! Thank you!

Feel free to visit my webpage :: facecheck15.posterous.com/hamoriden-was-tun-empfohlene-Lekture

Anonymous said...

There is certainly a great deаl to leaгn about thіs ѕubjеct.
I lovе all оf the poіntѕ yοu have maԁe.


Feel free to surf to my wеb-sitе cellulite

Anonymous said...

Ηello, I thіnk yоuг ѕite might be having browser compatibility issues.
When I look at уour website in Opеra, it lоokѕ fine but when opening in Ӏntеrnet Explorer, it
hаs some ovеrlaρping. I just wanted tο
give yоu a quiсk heads up! Other then that, terrific
blоg!

Аlso visit my blog post :: teen acne treatment

Anonymous said...

If уou аre going foг finest contentѕ likе mуself, only vіsit
this sіte dailу for thе reаѕon that
іt giveѕ quality contents, thanks

my website Prolonging Ejaculation