Student *addToHead(Student *node, int data) {
Student *temp;
temp = createNode(data);
temp->nextPtr = node;
return temp;
}
This code does not insert to the head of a linked list I have no clue why. I am not using a dummy node at the start of the linked list.
Here is my entire main function:
int main(void) {
Student *test = initList();
int searchTest = 0;
test = addToHead(test, 3);
test = addToHead(test, 2);
test = addToHead(test, 1);
test = addToTail(test, 4);
test = addToTail(test, 5);
printList(test);
searchTest = searchAge(test, 4);
printf("%d\n", searchTest);
test = freeList(test);
}
Here is what is being output:
4
5
0
Free was successful
The tail is being correctly inserted but not the head.
Here is the code for tail
Student *addToTail(Student *node, int data) {
Student *temp;
temp = createNode(data);
temp->age = data;
if (node == NULL) {
node = temp;
} else {
while (node->nextPtr != NULL) {
node = node->nextPtr;
}
node->nextPtr = temp;
}
return node;
}