 This is Argithi, R. V. Argithi from WD Sharapur. Today we will look the topic how to insert a node at specific location in W linked list. In the earlier videos we have seen how to insert a node at the beginning of the linked list, then how to insert linked list at the end in a W linked list. So we will focus at specific location in a W linked list, how to insert a node. The learning outcome of today's session, students will be able to explain the operation of inserting a node at a specific location in a W linked list. As we know this is an example of W linked list in which three nodes are there and each node there is a link between two nodes that is one is forwarding and one is reverse, backward direction. So these two nodes, these two links are connected to each other. So this is an example of W linked list. Now the types of insertion, as I said we have seen already how to insert a node at the beginning, how to insert a node at the end and today we will focus how to insert a specific location. The algorithm, initially what we will do, we will create a new node, then we will read the data to be inserted in the node. Then secondly we will check whether the list is empty or it is having some nodes. How to check the list is empty? If suppose the head is equal to null that is the address of the first node is equal to say null then we say that it is empty. If it is empty then whatever the created node is there, that node will be the first node of the list. If it is empty then what we will do? We will assign, in a W linked list we have two pointers, one is previous pointer and one is the next pointer. Those both will be made as a null. And next we will give the address of new node to the head. So that the head will, head means the starting address or the address of the first node in the list which is hold by the pointer head. So that address will be assigned to head if it is first node. This is third step. In the next step, in the next step if suppose it is not empty, if it is not empty means some nodes are existing the list then we have to traverse, we have to traverse to the particular location. So that, for that we will have to use the two temporary pointers, temp1 and temp2 and we have to initialize the value of head to temp1 pointer. So that means that now temp1 will hold the address of the starting address of the, or the first address of the first node in the list. Now we have to move the temp1 till it reaches to the node after which we want to insert the next node, new node. If it is reached to the last node, in case if it is reached to the last node means we could not keep the specific location then we have to display that the node cannot be inserted into the list. If suppose we get a specific location then what we have to do? We have to temp1 next, we have to assign temp1 next to temp2 so that temp2 will hold the address of the next node of temp1 then new node to, we have to assign new node address of new node or the value of new node to temp1 next so that the new node and new node will be connected to the, or it can be inserted at a specific location. Then temp1 to new node previous then temp2 to new node next and finally the value of new node should be assigned to temp2 previous. Now graphically we will see this is the creation of node, the node is created then initially the head is equal to null then insert the data for that this is the creation of node for that we declare previous, next and new node and head should be initialized to null initially. Then in function insert we will allocate the memory with malloc function that address will be assigned to new node. So here the address of this node will be stored, now read the data which is to be inserted into the node. So the data 10 will be read over here with this value we have taken from the function that is assigned to new node data, this is new node data, this is new node previous and this is new node next. So new node previous and new node next should be made null because it is a first node and finally we have to assign the value of head to this over here so that the value of new node should be assigned to head. In this slide these two are assigned as a null previous and next then head is assigned head is assigned the value of new node so that this is the first node of the list. Now the next part is that this is okay for when the new list is empty. If list is not empty this is the code for when the node is empty. So what we are doing new node, new node previous and new node next we are making assigning as a null and we are assigning the value of new node to head so that this will be connected over here or this head will hold the address of this particular node else means if suppose that node having some nodes okay then what we have to do means already some existing nodes are there then what we have to do we have traverse from first node to the node at which we want to insert the new node. So for that we will declare temp1 and temp2 pointers using these two pointers we will identify the location of the list and we will initialize the value of temp1 to head so that temp1 initially will hold the address of the first node and we will search where we want to insert the new node. Now temp2 which is not still vacant will traverse from temp1 to temp2 and so on okay with this particular look we are moving the temp1 till it reaches the node after which we want to insert the new node once it reaches to the last node display node not found this is the code for now we will see now if suppose it is not reached to the last node okay then it has reached to the particular location this is the location where temp1 is there so what we will do we will assign the value of temp1 next that is this 300 to temp2 300 is the address of this particular node so that is assigned temp2 will be hold over here and temp1 will hold the address of the location after which we want to insert the node so assign temp1 next temp1 next means this 300 should to temp2 that is over here then next is what new node we have to assign new node to temp1 next new node okay temp1 next is temp1 next is 300 that should be assigned over here then temp1 to new node previous temp1 this is temp1 okay temp1 is 200 that should be assigned to temp2 previous and temp2 should be assigned temp2 should be assigned to new node next new node next is what this one okay and finally we have to assign new node the value of new node to temp2 previous temp2 previous is this one so this is how we insert the node at a specific location now we have to find out what is the complexity of inserting node in a w linked list take a pause and try to give the answer the answer is o of n in the worst case the position to be inserted may be at the end so hence we have to traverse through the entire list so we have to the correct position we will be getting after o of n these are the references I hope you understood how to insert a node at a specific location thank you