Your problem is here:
void insert(node *previousNode, int num) { previousNode = new node; previousNode->nodeNum = num; previousNode->next = NULL; } insert(head, 20);
Here is what this bit of code does: previousNode = new node; makes a pointer to node and assigns this pointer to the previousNode. PreviousNode started as a copy, now it points to something new. Now you are assigning values to the new node. In other words, this implementation of the insert is not inserted.
What you want to do is something more:
void better_insert(node *previousNode, int num) { node *post_node = new node;
What it is: after creating a new node and pointing to it with a new pointer, we give it a number. The next thing is to figure out where the pointers point ...
let it pretend that we are waving some nodes in a linked list. All lowercase letters are pointers, ok?
a->next = b
now let's say that we want node x to come after a and have the number 10 ... we call `better_insert (a, 10)
post_node points to a new node (our node x) and is assigned 10. cool ...
we want:
a->next = x x->next = b
we have:
a->next = b x->next = null
the last two lines of the function just shuffle the material until it fits the bill
So in more detail ...
we have:
a->next = b x->next = null
therefore we call:
post_node->next = previousNode->next;
now we have: a-> next = b x-> next = b
now we call:
previousNode->next = post_node;
and in the end we get:
a->next = x x->next = b
or in other words:
a->next = x a->next->next = b