khuang8493
12/6/2018 - 9:18 PM

## Insert new node into single linked list

``````Insert a new node into the linked list:

So we will make a function that does the insert operation. And because we always insert the new node at the beginning of the linked list, therefore the new inserted node will become the new head of the linked list. So this function will return a pointer which points to the new head of the linked list.

sllnode* insert(sllnode* head, VALUE val); // obviously inside () are the pointer that points to the head of the linked list, and the value that we want to insert into the new node.

It will look like something like this:  list = insert(list, 12);

The reason why we would prefer to insert at the begining of the list is because: to be able to access each node in this linked list, we always have to start from the head of the linked list because the only way we refer to a linked list is the pointer that points to the beginning of the list. Like mentioned before, the difference of array and linked list, is the for linked list we always start from the pointer that points to the beginning of the list and then follow the chain to reach each node inside the list. We can't get random access of nodes like in an array. For an array we can say Array[8], so we can directly access a member in the middle of the array. But we can't do that with linked lists. We have to start from the very beginning, and then track down one by one.

So because of the above reason, if we want to insert a new node at the end of the list, we would have to start from the beginning, and go through the whole list and reach the end. So it's like taking "n" steps to complete this operation. But if we insert at the beginning of the list, we can do that right away.

So now, here are the steps involved for insert a new node:

a. Dynamically allocate space for a new sllnode.
b. Check to make sure we didn't run out of memory.
c. Populate and insert the node at the begining of the linked list.
d. Return a pointer to the new head of the linked list.

One very important thing that you have to consider for a linked list, is to arrange the pointers in the linked list in the correct order. The correct order is very important. If the order is not correct, we could end up "orphaning" some of the nodes.

In case of inserting a new node into the list, first we malloc() a memory space for the new node and make sure the pointer doesn't return NULL. Now we have this new node with it's own memory address. Do we first move the pointer that points to the head of the list to this new node? Although we have a new head pointer, but after we have done this, we have lost the record of the memory address of the old head of the list. The "next" section of this new node is suppose to point to the old head of the list, but now we don't know what that memory address is anymore. So now we have orphaned the new node.

So the correct order of doing this, is to have "next" section of the new node points to the old head of the list first. And now this new node is linked with the rest of the list. After that, we move the pointer that points to the head of the list to the new node. So now we have a new memory address for the pointer that points to the head of the list.``````