adding linked lists

I am trying to add every node in a linked list together to get a new linked list. My program compiled, but when I tried to execute the add method I got a Segmentation fault. I know that this means I screwed up my pointers somehow, but I have no clue how. Here's my add function.

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
27
28
29
30
31
32
33
34
35
36
37
void LList :: Add (LList &Num, LList &New){
        //PRE:
        //POST:

        listnode * temp;
        listnode * temp2;
        listnode * temp3;
        int remainder = 0;
        int num1;
        int num2;
        int sum;
        int base = 20;

        temp = new listnode;
        temp2 = new listnode;
        temp3 = new listnode;
        temp = head;
        temp2 = Num.head;
        temp3 = New.head;
        
        while (temp2 != NULL){
                num1 = temp -> data;
                num2 = temp2 -> data;
                sum = (num1 + num2 + remainder);
                remainder = 0;
                if (sum >= base){
                        sum = (sum % base);
                        New.InsertTail (sum);
                        remainder = (sum / base);
                        }
                else 
                        New.InsertTail (sum);
                temp = temp -> next;
                temp2 = temp2 -> next;
                temp3 = temp3 -> next;
                }
}
I don't know what's going on inside listnode contructor, but where you are actually assigning value to temp(/2/3)->next?
closed account (D80DSL3A)
JockX wrote:
but where you are actually assigning value to temp(/2/3)->next?

On lines 17-19, where the pointers are re assigned and the new listnodes are lost.
Right (it is true for temp2 and temp3, but temp = head is not defined in this scope). Anyway, the while loop only check if temp2->next is not null. Can it be the case that temp->next or temp3->next run into null before
temp2->next does (lines 34-35)?
Last edited on
That most certainly could be the case, but I assumed it wouldn't matter and would simply add nothing. Temp3 is for my New object, which is where I am trying to store the sum of my Native Object and Num. That's what I am trying to do anyway.
closed account (D80DSL3A)
It could be crashing because temp becomes NULL on line 33, then it's dereferenced on line 22. This would occur if *this (the object calling Add ) has fewer nodes than Num does, as JockX has pointed out.

If *this has more nodes then instead of crashing, the extra nodes get ignored and a wrong result is produced. This is also bad.

temp3 is probably iterating OK, but it serves no purpose in the program as it isn't being used to do anything (it shouldn't be needed to make InsertTail() work right, if that's what you're thinking).
Also, lines 14-16 are pointless. See my previous post.

Does it crash when *this has more (or equal) nodes than Num?
Last edited on
It crashed with fewer. I haven't tried it with more. I'm trying to conquer one problem at a time haha.
Topic archived. No new replies allowed.