Linked list question

So I was implementing a function that adds data to the tail of the linked list so I tried 2 things one of them worked and one of them doesn't work(crashes)

so here is what works:
1
2
3
4
5
6
7
8
9
10
11
12
void add(int data){
node *temp = head;
while(true){
    temp = temp->next;
    if(temp->next == nullptr) break;
}
node *temp2 = new node;
temp2->data = data;
temp2->next = nullptr;

temp->next = temp2;
}


and here is what crashes:
1
2
3
4
5
6
7
8
9
10
11
12
void add(int data){
node *temp = head;
while(temp != nullptr){
    temp = temp->next;
    //if(temp->next == nullptr) break;
}
node *temp2 = new node;
temp2->data = data;
temp2->next = nullptr;

temp->next = temp2;
}



What I don't understand mainly is, what is the difference between temp != nullptr and the if condition:
if(temp->next == nullptr) break;

You might call me dumb but the reason I am asking this is because for this function for example:
1
2
3
4
5
6
7
8
void print_list(){
node *temp = head;
while(temp != nullptr){
    cout << temp->data << " ";
    temp = temp->next;
}

}


We use the condition (temp != nullptr) and it works fine, but then using it in the add function doesn't work. Maybe I am missing something? Hope someone can explain this clearly ! thanks ^^


Also here is the entire program incase these code snippets are not enough.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>


#define fl(i,n)    for(int i = 0; i < n; i++)

#define ll   long long
#define nl   endl
#define pb   push_back
#define mp   make_pair
#define PII  pair<int,int>

#define EPS  1e-9
#define INF  1e9


using namespace std;

struct node{
int data;
node *next;
};
node *head;

void insert_to_head(int data){
node *temp = new node;
temp->data = data;
temp->next = head;
head = temp;
}


void insert_to_n(int data, int n){
node *temp1 = new node;
temp1->data = data;
temp1->next = nullptr;
if(n == 1){
    temp1->next = head;
    head = temp1;
    return;
}

node *temp2 = head;
for(int i = 1; i < n-1; i++){
    temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}

void add(int data){
node *temp = head;
while(true){
    temp = temp->next;
    if(temp->next == nullptr) break;
}
node *temp2 = new node;
temp2->data = data;
temp2->next = nullptr;

temp->next = temp2;
}

void print_list(){
node *temp = head;
while(temp != nullptr){
    cout << temp->data  << " ";
    temp = temp->next;
}

}
int main()
{
    head = nullptr;
    insert_to_head(1);
    insert_to_head(3);
    insert_to_head(4);
    insert_to_n(5,3);
    insert_to_n(7,1);
    add(8);
    print_list();



    return 0;
}


EDIT: Also what does EXACTLY does saying temp->next = temp2; mean ? like how does it work ?!
Last edited on
while(temp != nullptr)
at the end of that loop, `temp' will be null.
when you do temp->next = temp2; you are dereferencing `temp'.

`temp' is null and you are trying to dereference it.


Compare against the first snip, that may be rewritten as
1
2
while(temp->next)
   temp = temp->next;

that ends when `temp->next' is null, but temp is valid1 and points to the last cell of your list.


1 as long as `head' wasn't null, which it is at the start of the program.
so you need to call insert_to_head() first, quite error prone.
@ne555 also got a question, you said
when you do temp->next = temp2; you are dereferencing `temp'.


So what does this part mean :

head = temp;

What exactly is being assigned to head?

`head' is a pointer, so you must be assigning a memory address.
`temp' is a pointer, so it must be holding a memory address.
after head = temp; they would both point to the same memory address.


Try to reformulate because I don't quite understand your question.
Topic archived. No new replies allowed.