click_here
- Joined Sep 22, 2020
- 548
You need to go to the end of the list before you add the new node - Have another read of this...Any idea how to point to next node not previous node ?
Also, you have a memory leak - Every bit of memory you 'alloc' you need to free.With your adding function -
'malloc' a new node. If successful (not null), give it it's int value. (Later, you want to return a failure code on fail so the code can try and recover, but hold back for now)
If the head node is empty, put the new node as the head.
If the head is not empty, start at head and iterate through the list until you find the last node. (i.e. make a temp node pointer called "current", point it to the head, and while current->next != NULL, current = current->next)
Once you are at the last node, pointed to by current, set current->next to your new node, and then make the ->next = NULL
Hope this helps![]()