So I have been trying everything I can think of to insert a node at the end of a linked list. I dont want to give all my code since this is an assignment but I have spent hours trying to get this working. I feel so close like its only 1 or 2 lines away.
newPtr has already been created with the values being inserted and it is pointing to NULL
Code:
if(test != 1) { /*insert new node into appropriate place in list*/
head = *sPtr;
previousPtr = NULL;
check = 0; /*this is used to check if there is at least 1 node with a value*/
while(head != NULL) {
previousPtr = head; /*move through list until the end*/
head = head->nextPtr;
check++; /*increase check to show at least 1 node used*/
}
if(check > 0){ /*if there exists a node in the list, put new node at the end*/
head = newPtr; /*shouldnt head be pointing to NULL after the loop so this should be adding it to the end?*/
}
else /*if there are no nodes being used, put a node at the beginning*/
{
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
}