Adding 2 linked lists

The program adds numbers within two linked lists. My programs does that but the remainder from each number added isn't going to the next number. How do I fix this?
my output:

00234321 + 87954323 = 87199755


what it should be

00234321 + 87954323 = 88188644


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 int addLists(Node *top1, Node *top2, ofstream& out){
    Node *result=NULL;
    Node *temp=NULL;
    Node *prev=NULL;
    int sum=0;
    int rem=0;
        while((top1!=NULL)&&(top2!=NULL)){
            sum=rem+(top1->data)+(top2->data);
                if(sum>9){
                    rem=1;
                    sum=sum%10;
                }
            temp=createNode(sum);
            if(result==NULL)
                result=temp;
            else{
                temp->next=result;
                result=temp;
            }
            top1=top1->next;
            top2=top2->next;
        }

        printResult(result, out);

}


0+8= 8 = 8   | 8
0+7= 7 = 7   | 7+1
2+9=11 = 1   | 1
3+5= 8 = 8+1 | 8
4+4= 8 = 8+1 | 8
3+3= 6 = 6+1 | 6
2+2= 4 = 4+1 | 4
1+3= 4 = 4+1 | 4

Two issues:

1. You do start from the most significant digit and progress to "right". You should start from the "1+3" and progress "left".

2. When you have used the remainder, it still stays as 1.
I've inserted the numbers at the head. Are you saying I should insert them at the tail instead and set remainder to 0 at the end of the loop ?
You have to process the pairs in certain order in order to carry the "remainder" to the "next" pair. How you do that is up to you.

Which would be more logical "the end of loop" or "between lines 8 and 9"?
Topic archived. No new replies allowed.