Transcript for:
Inserting a Node in Linked List

In this presentation, we will learn how to insert a node at the end of a single link list. So, let's get started. Let us suppose we have this link list already available with us and this is the head pointer which is pointing to the first node of the list. And let us suppose that we have this node which we want to insert at the end of this list. I am assuming here that the address of this node is 4000 and there is some pointer temp. which will contain this address. So, here the temp contains this address 4000. We have the ptr pointer which is pointing to the first node of the list and we will use this pointer for traversing purpose. We will traverse this list until the end node is reached. And after reaching the end node, we will update the link part of this node by this address 4000 and the way which we will do this is by using this code. ptr link equal to temp. By using just this line, we can update the link part of this node by this address that is 4000. This is required because we need to make this node the end node of this list, right? So, this link part should get updated by the address of this node. ptr link equal to temp means we are accessing link part of the third node and we are updating it by this address 4000. So, this will get replaced by address 4000. This means that this node becomes the end node of the list. Let's see how to write a program in order to add this particular node at the end of the list. And here is the program. We have this main function of course and this is not a complete code, let me tell you. I am simply calling this addAtEnd function because our focus is to understand how to add the node at the end of the list. Right? So here we have this head pointer which is pointing to the first node of this list. We are assuming this, that this list is already available with us. We have some code which will create this list and this is the pointer head which is pointing to the first node of the list. And what we will do here, we just pass this head pointer. Apart from this, this addAtEnd function requires the data part, right? That is the data part of the end node. Here in the addAtEnd function, we will create the node, we will add the data to it and of course, we will make that node the end node of the list. So, here is the function which will do our job. And let's try to understand how this function works. We have this struct node starhead and integer data. You can see over here, these are the pictorial representations of head as well as data. Data contains this value 67. and head is the pointer pointing to the first node of the list. We will create two different pointers ptr and temp and these are the pictorial representations of ptr and temp. By this step, we are trying to assign the head to ptr. That means we are trying to assign this address 1000 to ptr. Right? ptr is now pointing to the first node of this list after this step. Right? Now, with the help of this line, we are actually creating a new node and this is how we are creating a new node which consists of a data part as well as the link part and of course, I am assuming here that the address of this node is 4000. And there is a temp pointer which contains this address. And that is what is shown here. We are assigning the address returned by this malloc to this temp. So, temp now contains this address 4000. After this, we are updating the data part of this particular node with the data, that is this data 67. So, we will replace this data by 67 and we will update the link part that is here. We will update this by null. After this, we have this step. While ptr link not equal to null, ptr equals to ptr link. This step is required to traverse the list, right. This is the code which we should write to traverse the list. We are using ptr pointer to traverse the list as we have already discussed this, right. Now, instead of using ptr not equal to null, we are using ptr link not equal to null and you will understand this why I am using this, okay. Here ptr link not equal to null means ptr is now pointing to the first node of the list. PTR link gives this address that is 2000. And we can see over here, this is not null. That is why we get inside this while loop and we will update PTR by PTR link. That is we will update this PTR by this address 2000. Which means now it is pointing to the second node of the list. After this, we will again move to this point that is PTR link should not be equal to null. You know that the link part doesn't contain null. So, we will get inside, we will update the PTR by this address that is 3000. So, now it is pointing to the third node of the list. After this point, We will check this condition once again. Is ptr link equal to null? Yes, now this is equal to null. We stop at this point. We will come outside of this loop. And at this point, we will update the link part of this node by this address 4000. After updating the link part, this node is now pointing to this node. Here you can understand that why I am not using ptr not equal to null. Because here, I don't want to update this by null. I want to stop. when I reached the end node of the list. That is why I should replace this ptr by ptr link. Okay, instead of checking this condition, ptr not equal to null, I am checking this condition, ptr link not equal to null. So, this function is capable of adding a new node to the end of the list. So, this is the complete program. Of course, you should understand that here, I am passing the head directly. We are assuming here that head is a pointer pointing to some single linked list. Okay. So, this is our assumption. Apart from this, this code is complete. And this is the linked list so obtained at the end. After executing the program, this is the linked list we obtained. Okay friends, this is it for now. Thank you for watching this presentation.