I just want to know if there is and difference between these two notations:
First example:
struct node{
int data;
node* next;
};
void insert(node* list){
node* n,* last;
last = list;
while(last)
last = last -> next;
n = new node;
last = n;
n -> next = NULL;
cin >> n -> data;
}
int main(){
node* list = new node;
list -> next = NULL;
insert(list);
return 0;
}
Second example:
struct node{
int data;
node* next;
};
void insert(node* list){
node* n,* last;
last = list -> next;
while(last)
last = last -> next;
n = new node;
last = n;
n -> next = NULL;
cin >> n -> data;
}
int main(){
node* list = new node;
list -> next = NULL;
insert(list);
return 0;
}
So I wonder if there is difference between notation lie: in first example:
last = list;
and in second example
last = lista -> next;
And what does node* list do/represent in void insert(node* list){} Is that just a pointer to whatever list, a pointer that points to the list in the main, or what?
Your above explanation stands correct but let me also add one more point, Suppose your
list = NULL;
void insert(node* list){
node* n,* last;
last = list -> next;
while(last)
last = last -> next;
n = new node;
last = n;
n -> next = NULL;
cin >> n -> data;
}
Then in the above case you are trying to access list->next
which is next of 'NULL' thus it will give you runtime error because you cant access next of nullptr as it is not defined.