1 2 3 4 5 6
|
NodePtrType newNode, prev,current; //pointers to the new node and for
//traversing the list
// Create a new node to hold the record. Be sure to put
// the data from newData into the newNode.
newNode->data = newData;
|
If newNode is a
pointer, then what does it point to?
Looks like you dereference an uninitialized pointer. That is
undefined behaviour.
1 2 3 4 5 6 7 8
|
current = head;
prev = NULL;
while ( current->next != NULL && current->data.acctNum < newNode->data.acctNum)
{
current = current->next;
}
newNode->next = current->next;
current->next =newNode;
|
What is the purpose of 'prev'?
Lets take list {A, C}. We want to insert B.
List is not empty and B can't go before A, so we go to the
else
:
1. current = A
2. There is something after A and A<B
3. current = C
4. There is nothing after C
5. B->next = NULL
6. C->next = B
In other words, we got list {A, C, B}. We were supposed to get {A, B, C}.