I tried to make a function that would add a node to the beginning of the list, and then change the variable "head" (holding the previous beginning of the list) to contain the new node.
void addToStart(node * n, node * first){
printf("[Before adding] Node: %d, First: %d\n",&(*n),&(*first));
n->next = first;
first = n;
printf("[After adding] Node: %d, First: %d\n",&(*n),&(*first));
}
int main(){
node * head = createNode(0);
printf("This is the location of head: %d\n",&(*head));
node * fred = createNode(2);
addToStart(fred,head);
traverse(head); //Displays the list starting from the given node
return 0;
}
This is the output:
This is the location of head: 10113040
[Before adding] Node: 10113072, First: 10113040
[After adding] Node: 10113072, First: 10113072
(0)[10113040]->NULL
The problem is that I expected the function to change what head
was pointing at, but in reality nothing has changed.