 Hello friends, I am Sanjay Bukta. I welcome you on Sanjay Bukta textbook. In this video, I am going to explain how that insert an item into a link list. So basically in this video, we will tell you creation of link list with the help of C language. So first of all, I am going to tell you how we can create a structure so that we can create room with the help of the structure. Right. So, so this is a data link when name of structure is node. Inside this, I'm taking a variable of integer line and structure node pointer link. So basically this type of structure where the type of structure is also written inside it is known as self-referential structure. So this type of structure is known as self-referential structure. So whenever you will be creating a mapping block or you will be looking at mapping with the help of structure. So what will happen? It will be provided to two memory spaces, one is of integer and one is of type structure node. And that will be a pointer that can hold entrance of another structure that will be related to this data mesh. So what will happen? It will create a node which will be divided into two parts. First part will be known as info. Setting part will be a pointer and it's name will be link. So through this structure, this node will be created. Now, whenever you will be creating one more node, whenever you will be creating one more node. So basically it is known as node number one. This is node number two. So whenever you will be creating this node, so it will also be having info and pointer link. So what will happen? This nodes base address or building address will be stored here. So for example, it's base address is 1001, it's base address is 2001. So this is the information on value part, integer type. This pointer link will be stored here. Let's say it is 20 and this will be having number. So this way if you are on this node, so through this node link pointer you can move to this node. If it is pointing to another node, so you can move to better. So this way basically the concept of link is stopped and to create these nodes we need to reuse this like a structure. So now you might be thinking like how we can create the mapping blocks. So for that purpose, you will be writing a function. So after declaring this structure, you can write a function and name of that function will be get. So get node is a name of function. It's written like a struct node pointer. So basically this get node function will be created in a node. This get node function will be created in a node and this get node function will be regarding the base address of this memory that is allocated to this get node function. That's why its written type is struct node pointer. Now inside this function, I'm creating one more pointer that is struct node pointer t. Now I'm going to allocate memory to this key pointer. So struct node pointer for memory allocation and using add-off function. Inside add-off and passing size of now size of struct node. So we need to create memory for this structure. So it's data type is struct node so I'm passing that. So size of this can be total amount of memory required to this struct node. Then this memory will be allocated at this address will be like ask it to struct node pointer and this address will be assigned to t pointer. And then I can write return t. So wherever you want to insert a particular node at any position in your list. Whether it is first location or it is last location or in the middle of the list. Then before insertion you need to call add-off function. Add-off function will be creating a node and it will get a base address of this t pointer. So this way we can create a particular node in c language. So I hope you understood this function and add-off is used for dynamic memory allocation. So whenever memory is required you can go on this function. And internally add-off will be allocating memory equal to the size of struct node. So this is about add-off. Now we are going to discuss three things. So first I'm going to tell you how to write add-off function. Add-off means whenever you will be calling this function the node will be inserted at the first position of the list. Second will be at last. So in this case we will be inserting node at the last position and third will be at after. So in this case we will be inserting a node after a particular position that is specified by the user. So right now I'm going to write the function for add-off. So I'm going to add these things. So after implementing add-off again I will tell you how this link list will be created. So now I'm going to write add-off function. So it's written I will use that node pointer. It's name will be add-off. Let me write it. So add node pointer. Name of function is add-first. It will receive parameter as struct node pointer. So it will be stored into start and the item that we are going to insert. And then here we will give it its definition. So I am using this as an add-off. So let's assume it is written here. And sign-off right. So we are going to use this case that's why I have modified this. So now what we are going to do? First we need to create a node so that we can insert that node into the link list. And this add-first function will be called from main function. So main function will be having a pointer of add-struct node. So initially that pointer will be like this. So in main function you can create a start node pointer. Let's say its name is start and initially it will be having null. So we are passing that into a start pointer and item that we are going to insert. Then struct node pointer new. So this is another pointer that will occupy memory. So now we need to call this get node function. So new get node. So this get node function will be called it will create a memory block. That memory block address will be added into key. So key will be returning that address and that address will be storing to new. So for example, let's say this is start and this is new. So new will be pointing to an active node. It's base address is 1001. So this way it will be. So new is pointing to this memory block which is divided into two parts info and date. And it is storing into new and start is null. Now what we need to do? We need to insert this pointer to this pointer. So that the new node will be inserted into our actual image. So before that we need to write if new equals null. So if new is null it means memory is not updated. So you can print that. Or whatever message you want to display you can write. So function will be calculated here and the remaining statement will not be executed. But if this condition is false then it means memories are located. And we can insert that a node into the image. So first what we need to do is to fill this information part. So you will write new info equals to item. Now again you need to notice one thing. Whenever you access member of the structure to its pointer. So new is a pointer of structure type and info is the member of structure which is mentioned here. So if you want to access member of structure to its pointer then you need to do this arrow operation. And how you can create this arrow? iPhone and then give the answer. So let's see active scan. So this scan will just go in. Now what we need to do? We need to insert this node into this radius. So let's say this link is blank. So in that case this node will be passed over and its next will be nothing. But if link list is already having some nodes. For example because not enough and if it is having one more node. So what will happen? Its address will be here. So what we need to do? We need to make this node as passed over. So this node's address will be here. And the address of this node will go to here. So that it can become second node. So now we need to write the statement for this. So what I will do? New link equals to start and start equals to here. So this we need to do. So for example its base address is 2001. Which is stored here. This is a node which is already available to link list. And we need to insert 1001 nodes before this. So with this new link will be start. So start is having 2001. So it will move here. And new will go to start. So new is having 2001. So it will move here. So now start is pointing to 1001. And 1001 link is pointing to 1001. So this gives you break. And it will point to this. And this will point to this. So this way you can see your node that you inserted right now. At first position is now on first position in the node which was implicated earlier. And which was on first position right now it is on second position. And this new is temporarily implicated in this function. So it will be of no use right now. And from here you can return start. Because start is now pointing to new location. So you need to return the start. So that it can be received in the beginning. So this way I implemented and first logic. So I hope you understood how we can insert a node at the first position of the node. So after this now I am going to write a second method that is related to add last. So in that case we are going to insert node at the last position of the link is right. So in this we inserted first position now we need to add it to the last position. And then we need to insert the node right. So I am going to erase this. So now I am using a second function that will be inserted at the end of the list. So for our demonstration purpose I already created this. So this is first node base address is 1001. So start second node third node. So first node link is having address of second node. Second node link is having address of third node. Now we need to insert new node at this position right. So written name is struct node pointer method name is add last. And it will be receiving two parameters for struct node pointer, start and end item. So item will be the value that we are going to insert. Now I am going to take care of two pointers for struct node pointer new. And second one will be feature right. So new for creation of new node that will be itself and feature for creating a new. So now new equals to get new. So this method will be executed it will create a new node and its address will be available in the new pointer. So for example this is new pointer new node will be created. Let's suppose it's base address is 4001 and it will be available in the new. Now the address which is available in the new we need to store in the link of 3001. So third node link will be having address of this newly created. That is right now available into new. So before insertion we need to check if new is covering first from now. So we can print overflow later right. So if this happens and this will be created and your method will be terminated. If it is false it means memories are available. So we already know this node will go to last location right. So we can directly write some statements. New of info equals to item and new of link equals to null. Because this node will be placed at last position for its link will be null and its info will be having the item. So let's say item is 14. So 14 will be stored here and this will be null. But these two statements right. Now we have two options. First if your link list is empty. If your link list is empty it means you are inserting this node for the first time. So in that case your start will be null. And second is your link list is already having various nodes right. So after this we can check if start is equal to null. If start is equal to null. So we can directly assign the value which is available to new to start. So you can write start equals to new. So remember this. This is the case where your link list is empty. It is not having any particular address. So we simply assign value to start. Now as if link list is not null. So right now in the case link list is having three nodes. So now what we need to do? We need to traverse each node and we need to move to this location. So that we can assign address of new at this location right. So now I am assigning pdr equals to start. So start is pointing to 1001. So I have one more pointer that is pdr. So pdr will be pointing to 1001. So for example this is pdr and it is pointing to 1001 right. Now I am writing a new by pdr link not equal to null. So if pdr link is not equal to null then we need to iterate pdr equals to pdr link. So this is a loop which will repeat only this line right. So right now pdr is 1001. Its link is not null. So pdr will move to its link. So pdr is 1001 and its link partner is 2001. So next time it will be having 2001. Now check pdr link not null. So pdr link so 200 and its link is 3001. That will be assigning to pdr. So this time value of pdr will be 3001. Again check pdr link so 3001 is null. So this loop will be permanent right. So pdr is pointing to 3001 this loop. Now what we need to do we need to assign a new at this position. So we get simply right after completion of this loop pdr link equals to null. And remember one thing why loop is repeating this line only. And this is separate from the why loop after completion of this loop this statement will be executed. Then you get knows as far and at last 2001 start. So this way I explain you how you can add a node at the last position of the link list with the help of this code. So again we use this get node function so that we can allocate memory. And then with that allocation of that memory we exerted a node at the last portion right. So I hope you understood two methods now. First was at first and second is at last. Now we have third and after right. So before starting that I am going to remove this then we will be at the static test. So now I am going to explain the concept of add after. So I have third method is add after so why add after. So this time it is why because we are not going to change the start. So we don't need to return this to main function. And parameters can be struck node pointer start in item and in position. So position will be the node after that we need to insert a new node. So let's say position is 2. It means after this node after this node here we need to insert a new node right. And in previous method where I created new node which was pointing to 4001 right. So this was the page and its address was 4001. So I didn't mention like this will be holding that as 4001. So new will be removed because new was created to that function. So now when we assign 4001 into this location. So it means this location is pointing to this. And this is the fourth node and it is containing another interlink. So here link list will be done. So this way we can connect that. So now what we need to do we need to insert new nodes but then these two nodes right. So again I need to create new more add. I need to put that nodes address here in between so that that can be inserted in between two nodes. So now I'm going to write the statements for that purpose. So start node pointer new and pointer media. So these two are pointers I created right now. So in this way it was I for I121. So I initialize with 1 and here I am assigning pdr and start. Then condition will be I have an position. So if I is larger position then let's move this way too. Then increment I plus plus and pdr equals to pdr name. I will be implemented by one and pdr will move to next node. Now inside this if pdr there were any questions. So let's say in this link list we have four nodes and you mentioned position as five. So in that case pdr will be pointing to null position at a particular time because fifth node is not available. So if pdr is null at any point of time before moving to a particular position that we are searching here. So you can write an address no such location or position and then you can write data. So this is the loop to which we can move to a particular position after which we are going to insert a particular loop. So let's say position is 2 and pdr equals to start. So this is pdr it is having 1001 this is start position is 2 I is 1 so it is true pdr is not null. So I will be implemented I will be true and pdr will be having pdr name. So pdr is 1001 its length is 2001 so pdr will be pointing to 2001 then I will be true. So this condition which it was it means finally pdr is pointing to 2001. So pdr is pointing to 2001 it means after this code we are going to insert a particular loop. So now after this loop what we need to do we need to create a new node getNode. So getNode will be a new node. So let's suppose this is new this is our new node its base address is 5001 which is available into new with the help of this. And here you can check if new the value equals to null. So again you can write print as 0. So I already explained to you this why we implemented at first and at last. So if memory is not available sufficient amount of memory is not available then new will be 100 null it means new node is not created. So you can write print as 0 and you can validate your method here. Now first we need to fill this. So I am writing new arrow info equals to item. So item let's suppose 50 people sir so 50 people store here new of info. So new is pointing to 5001 it is in position with 350. Now this node will be inserted here so what we need to do this node will become third node and this will become fourth. So this fourth node address will be here and this will become third node. So its address will go to second nodes link part right. So right now this node address is available here and we need to shift it here. So pdr is pointing to 2001 so through pdr link we can assign that address into link of new. So you can write new link equals to pdr link. So pdr is pointing to 2001, 2001 link is 3001 which will store here and this position is new link. So new link is now pointing to pdr link address 3001 it means this node will point to 2001. Now we need to place this node address here so that this will be connected to the link. So this location is pdr link and we need to assign new here. So pdr link equals to new. So new is having 50001 and here pdr link will be having 5000. So this node is disconnected so 2001 link is 50001 so it will move here. After this we are 50001 and 5001 link is 30001 so it will move to this. So this way you can see this node is inserted in this. After second position so this is third node this is fourth node and this is fifth node. So this way your third method is complete which is add after. So we inserted a new node after second node with the help of this node. So this way I hope you understood all three methods like how to insert a node on first position, how to insert a node at the last of the link and how to insert a node in between the link list. So this way I hope you understood all three methods that I implemented in the C language. Now if you want to watch how we can implement all the methods including that structure and with the help of main function. So if you want to watch that complete program so that program is available in another playlist that is related to DSA implementation using ID integrated development environment. And the link of that playlist is available in the description of this video. So you can open that playlist and you will find a video related to complete implementation of link list with the help of ID. So watch that where you will learn how we can execute this complete method. So this was the board explanation and that will be implementation of ID. So if you want to watch that video you can go to the description of this video. And if you want to watch algorithmic implementation of these things add first, add last and add after. So that playlist link is also available in the description of this video. So I hope you understood whatever I explained in this video. Thank you for watching this video.