I've been taking a course and I know how a linked list works, I've made several of them by myself as practice and they all work fine, however, I came across one that wasn't as obvious to me and took me a bit to understand.
When you have a node class where the constructor takes arguments to set the value and link vs when you have a constructor that does NOT take arguments.
Example: Node(const val = 0, Node* ptr = nullptr){link = ptr; value = val;} vs Node(){}
How do the two implementations differ below? assume head_ptr points to nullptr
void list_head_insert(node* head_ptr, const value){
head_ptr = new node(value, head_ptr);
return;
}
// yes i know you need to account for more than 1 node being there if there is
// assume there isn't and this is the first and only so far
void list_head_insert(node* head_ptr, const value){
head_ptr = new node();
head_ptr->set_value(value);
head_ptr->set_link(nullptr);
return;
}
in the first, is the new node created first and then head_ptr points to it after or does it point to it first before inserting values via arguments?
edit: i forgot to add & to pass head_ptr by reference in the argument, but i see why it should be there.