0

Pointer List difficulty

I just learned pointers and how to manually make a pointer list. I have something like this: struct node{ int no; node *next = NULL; } The algorithm to add an element to the end of the list looks something like this: nod *s = f; // f is the pointer that points to the start of the list while( s->next != NULL) s = s->next; s->next = new nod; s->next->no = x; //x is a random integer to add to the list Isn't it the same as this one: nod *s = f; // f is the pointer that points to the start of the list while( s != NULL) s = s->next; s = new nod; s->no = x; //x is a random integer to add to the list P.S. : The first algorithm works and the second doesn't. Why?

14th Nov 2017, 3:33 PM
Andrei CĂźrpici
Andrei CĂźrpici - avatar
2 Answers
+ 1
Thank you!
14th Nov 2017, 8:15 PM
Andrei CĂźrpici
Andrei CĂźrpici - avatar