Solved

# linked list sum function

Posted on 2006-04-06

Hello, I am adding 2 linked lists. These can be of different lenghts. Below is the sum function. But for some reason the program keeps crashing, and I guess i am missing smth or doing smth wrong in my fucntion. Would you please give me some comment on the function.

void Calculate::sum (Calculate &Number2)

{

int carry = 0;

digit *p1 = head;

digit *p2 = Number2.head;

while (p1 !=NULL && p2 !=NULL )

{

p1->item = p1->item + p2->item + carry;

if (p1->item>=10 )

{

carry=1;

p1->item =(p1->item)-10;

}

else

{

carry=0;

}

p1=p1->next;

p2=p2->next;

}

if ( p1->next == NULL && p2->next !=NULL )

{

while ( p2->next !=NULL )

{

digit *newdigit = new digit();

newdigit->item=p2->item;

p1->next= newdigit;

newdigit->next = NULL;

p1=p1->next;

p2=p2->next;

}

}

if ( carry==1 )

{

digit *newdigit = new digit();

newdigit->item=1;

p1->next = newdigit;

newdigit->next = NULL;

}

}