I'm trying to figure out how to use linked lists and i'm sort of understanding, however i've ran into an error. I used a youtube tutorial which was good, however he did not explain this code and I copied it from him and tried to understand it at my own pace, however I cannot really understand why it works this way and how to fix it.
if(firstnode == 0){
temp = addtolist(request2);
head = temp;
firstnode++;
}
else{
temp = addtolist(request2);
temp->next = head;
head = temp;
}
Add to list code
linkedlist *addtolist(char value[]){
linkedlist *result = malloc(sizeof(linkedlist));
strcpy(result->data,value);
result->next = NULL;
return result;
}
Firstnode is simply an integer to check whether or not the user has created a node or not. And request2 is just the value that i wish to store in the node. I am wondering as to how to get my linked list to be in the proper order when i print it, as i am moving the head everytime, what can I do to prevent this and link the list properly? Sorry if its a dumb question but I can't seem to understand how this works.