Gipsy
asked on
linked list sum function
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;
}
}
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;
}
}
ASKER
you mean if ( p1->next != NULL && p2->next !=NULL ) ?
I wrote it that way because if If p2 is not empty, then i want to copy the rest of p2 to the end of p1.
I forgot to mention that when i add the numbers, they are added with the least significant digit first. When i enter 200, the linked list stores it as 002. So if i want to ad 456 to 32
23
654
----
884, so 4 is just being added to the p1.
I wrote it that way because if If p2 is not empty, then i want to copy the rest of p2 to the end of p1.
I forgot to mention that when i add the numbers, they are added with the least significant digit first. When i enter 200, the linked list stores it as 002. So if i want to ad 456 to 32
23
654
----
884, so 4 is just being added to the p1.
Look 23 lines down from that if.
You'll see: if ( p1->next == NULL && p2->next !=NULL )
If you look in the scrope that follows you'll see a while loop. In the scope of the while loop, you'll see:
p1->next= newdigit;
That will be deferencing p1, when it is null.
You'll see: if ( p1->next == NULL && p2->next !=NULL )
If you look in the scrope that follows you'll see a while loop. In the scope of the while loop, you'll see:
p1->next= newdigit;
That will be deferencing p1, when it is null.
No it won't. My apologies. I haven't got my eyes screwed in right today! 8-)
OK, here's the problem.
When this loop exits...
while (p1 !=NULL && p2 !=NULL )
{
}
...wither p1 or p1, will be NULL.
You, however, deference them in the next line...
if ( p1->next == NULL && p2->next !=NULL )
... to look at their next values and that will be causing the crash.
My eyes are screwed in properly now I hope. :-)
When this loop exits...
while (p1 !=NULL && p2 !=NULL )
{
}
...wither p1 or p1, will be NULL.
You, however, deference them in the next line...
if ( p1->next == NULL && p2->next !=NULL )
... to look at their next values and that will be causing the crash.
My eyes are screwed in properly now I hope. :-)
ASKER
rstaveley do you have any suggestion, taking into account what i am trying to do with the code. Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
!= NULL perhaps?
If not, you'll go kaboom, when you dereference the pointer in...
p1->next= newdigit;
Can't comment about the logic otherwise, because I've only scan read it and it has been many years since I messed around with home-grown linked lists.