 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how to write algorithms to insert an algorithm in the interest. So we will be discussing three algorithms. So first will be adding any item at the first position of the linguist, second adding any item at the last of the linguist and third will be adding any element in between the linguist like after a particular quote. So we will be reading the position like after which note we want to insert the element. So that we will do in this algorithm. So right now I am going to write first algorithm that is for at first. So this is the name of algorithm and whatever terminology we are going to use that I am mentioning here. So these are the things that we are going to use in this algorithm. When you will be converting this algorithm into a program, so these terminology might change accordingly. So let's say if you are implementing this algorithm with C, so you will be using structure. If you are using seamless class then you will be using classes. So accordingly you will be passing the measurement. So here start with the pointer that will be putting the address of first move. So let me do those as well. So let's say this is the start and initially it is null. Then info and link, these are two parts of null. So I already explained this while I explained how to write algorithm to traverse each node that are available in linguist. So if you have not watched that video previous to this, so you can find link of that video in the playlist. So that link is available in the description of the video so you can follow that as well. Then we have away, so away you can consider that it is a pointer that holds base address of the free memory space. So we are considering these are the free memory blocks and these are linked together to some kind of addresses and this first location centers are available here. So let's say this is 5111 and it is 4111. And item is the value that we are going to insert into that node that we are going to create. So now I am going to write the steps so that you can understand how we can write algorithm to add up particular item at first position of the menu. So step number one, so in case of step number one, I am checking all your petitions. So if available equals to null, so available is basically pointing to the free space if there is no free space available in your system. So again it will be version of value and if available equals to null, it means you cannot allocate anything and you cannot insert any nodes into your link here. So you can simply write print overflow and exit. So this thing you also need to verify by implementing this algorithm or with any programming address that you have in a component of memory or free space. So that you can allocate that memory to the link list or not. So after this validation, I am writing step number two. So if step number first is false, then step number two will work. So step number two will do what step number two will do. It will extract a mapping block from this available memory space and it will be allocating to a new temporary point. So now I am writing rules. So set new equals to avail and avail equals to link of avail. So this statement that I have mentioned here, in case of programming, this will be done automatically. Let's say if you are using maVoc to allocate memory space for this file, but for reference purpose in algorithm, we need to show that we are accepting the space and we are going to allocate that to particular languages. So here you can see new is available. So I am creating one more pointer that doesn't need. We are assigning avail to avail. So avail is pointing to the base address of this free space. So that will be available here. And if we write avail equals to link of avail, it means avail will be pointing to this location and new will be pointing to this location. So this block is now extracted from the free space and its base address is available with the new. And let's say address of this location is 6w1 so it will be pointing to this. So now this is your free space and new is pointing to this location. And now what we need to do, we need to insert this location to this link list so that we can store that. So this way memory location internally happens. So I hope you understood this concept. So remember one thing also, physically you cannot move your memory location. You just need to store the addresses into different pointers so that you can say like if I have or I am holding a particular address, it means I have right on that memory location. If any other pointer is holding that address, then that pointer is having right on that memory location. So initially avail was holding 521. So this memory location was under this avail point. But now new is holding this address 521. So on this memory location, new is having the address. Okay. Now moving forward. So I am going to write scapula 3. So in case of scapula 3, I am writing set info of new equals to item. So whenever we expect this memory location and we assign base address, so we need to imagine that it is internally divided into two parts. So two parts means so let me explain this here. So this is the location I am going here. This is the first part, which is info. And this is the second part, which is link. So info will be containing the value on item and link will be containing the address of next possible. So here we are assigning item info of new. So info of new is this and let's say item is 16. So 16 will be assigned here. 16 will be assigned here. Then tab number 4. So this node will become the first node. This node will become the first node. So first node address always goes in after start. So right now I am writing link of new equals to start. So start is having null. So null will be stored into link of new. So link of new is this. So this location will be having null. And now we need to store this location to the start pointer. So new is having 5,000 months. Now this value will be stored here. And this link will be great. All you can say this is a temporary memory or temporary pointer that we use to expect memory from available to this space. And we assign that pointer into start vector actually. So now you can see this pointer. So I am moving this time from here and it is pointing to this and it is pointing to this. So here you can see start is having 5,000 months and it is pointing to this first location. So this will be inserted a node into linkless and first location. So this happened when linkless was empty. Now linkless is having one node. So now we need to understand like if we want to insert one more node before this 60 value. So how that will be happening. So again you need to execute this whole algorithm. So you need to check away if it is not going to be available. That means that new equals to again. So again a temporary pointer to be created that is new. This time it will be having value as 6,000 months. So here we have this available free space and away we will move to next location. So away we will move into this location. And this location is pointing to this link. Right. And it has been eliminated by two parts. So then you will be implementing this algorithm into our C or C clusters. So remember that whenever you will be in an empty space. So you need to create your structural class in such a way. And two landing blocks will be there. One will be info that is of each of those type. And second will be a pointer that will be holding address of next node. Right. So this way we can say it is node number one. Now we are creating node number two. And we need to insert node number two before node number one. So now these statements will help. So this time item is 15. And we need to insert into the fourth new. So the fourth new is this. So 50 store here. Now link of you will be having start. So start is having 5001. So 5001 will go to new link of you. So link of you is this. So it will be having 5001. Right. So it means 5000 rent here and you will be assigned to start. So new is having 6001. So it will go here. So now what will happen? It is pointing to 6001. So this connection dates. It is pointing to this. And it already did. So this node is now first mode. And its next part is having 5001. So this is this. So it is now. So now I will be moving this available space. So then you will be understanding this. So this start is pointing to this. And this is pointing to this. Because its next part is having 50001. Which is the base at this point. So this way I hope you understood. Like this node is now first mode. And this is taking over at its next part. So this way at first happens. So with this algorithm, whenever you want to insert any node at first location of the link list. So you can apply these steps. With the help of these steps, you can implement your function in C for C++. So that every time you know, it will be inserted to the first location. Right. So this way I hope you understood how we can perform at first operation. Now I will put in the last operation. So let me first is So now I raised a statement. So we are going to study at last. So the name of this will be at last. And step number 1 will be same. And this I am changing. So this is our first move 6.001. And this is our second move. So I am writing that properties so that you can understand that. So let's say this is first move 6.001. This is second move 5.001. It is having value 15. It is having address as 5.001. Value 60. This is connected with this and it is connected with this. And here we have a that is pointing to three spaces. Right. And let's say it is now pointing to this. Right. And we need to insert this available space at the end of this list. Right. So we are going to perform at last operation. And again, here you can see same parameters are available. So start it fully like that. That would be sufficient. If available that we can go to. And that's it. Because if we don't have any space that we cannot create a code and we cannot insert it into. Then we have that number too. So that new equals to avail. Then avail equals to link up away. So this way. A new point that will be there. It will be holding the address of this location. It will be expected. And let's say this is 2001. So now we need to insert this code at the end of this thing. Right. So now again, there will be two cases. Right. There may be chances you'll get places empty or there may be chances that the link list is having or those are already available. So we will see both the cases. So now step number three. So set info of new equals to item. So let's say this time I'm going to insert 40. So 40 will be stored here. 40 will be stored here. And we already know this node will be inserted at the last location. So it's linked. It means this will always be having malware. So we get set that already. So set link of new as none. Right. So this new location is fit. Now we just need to place this base address at the end. So let's say if you're. So after this node, after this node, this node will be coming. So 5,000 once next or link base address as 1,000. So that through 5,000 one, we can do 1,000 one. Right. And if your link list is blank, if your link list is blank or start is having none. So we can just assign this new to the start. So step number five. If start is equals to none. It means your link list is blank and we are inserting first node on it. So you can write that start equals to new. So in that case, whatever addresses of any kind of new will be open to start. And this node will be inserted to the new. So I hope now we can imagine this. So this is the first case. Second case. If you have a node already available. So that case is already available here. So in that case, you need to write else and inside else. You need to repeat them so that you can move to the last location. So I am writing that PPR equals to start. So we are using one more pointer so that we can move to the last location. Now I am writing repeat. So we need to repeat some set of steps while so now we need to figure out the condition. So condition will be link of PPR not PPR is to none. So link of PPR not PPR is to none. So now PPR is having start. Start as this. So PPR is pointing to this location. So it starts here. Now check link of PTR, so link of PTR is 5 of the month which is not equal to that. It means we can move PTR to next location. So here you can write that PTR equals to link of PTR. So link of PTR is 5 of the month that will be stored into PTR. So PTR will point to this location. Now again you need to check the condition of the link of PTR is not equal to none. So this time link of PTR is none. So this loop will not repeat half. Okay, so you can write or reference and now do. And after completion of this loop PTR will be pointing to the last node. So now what you need to do? You need to assign this address to this location. So this is link of PTR. So link of PTR equals to new. So this will be the next step. So in this address after completion of this loop you can write set link of PTR equals to new. So new is having to, sorry 1000 that will go to link of PTR. So here it will be having 1000. So it means now this is connected to this and this is having next address. So this way we inserted a node at the end of link of this. Now here you can write end of this. So this step number 5 is completed here and step number 6 you can write end. So this way this is setting algorithm that is for adding a node at the end of the ticker link. So now I am going to erase these. So now I need to explain this one after time changing this to adapter and here one more parameter is required address position. Like after which node we are going to insert one more node and let's say I am doing this. And this here is having 14 and now it is pointing to this. This is 2000, this is 3000. So this is available free space. So now let's say position is 2. Position is 2. It means after the second node I want to insert one more node. So new node will be inserted between 16 and 40. So between 16 and 40 I want to insert one more value. So for that I am going to write this algorithm that is adapter. So step number 1, step pdr equals to start so pdr is pointing to this first location. Now I am going to repeat a loop. So the big steps for i equals to 1, 2 position minus 1. So position is 2, 2 minus 1 is 1. So this loop will repeat one time right now. So pdr is already on this position. So if we repeat one time this loop, so pdr will move to this position. So here that is set pdr equals to link of pdr. So link of pdr, so pdr is basically pointing to this location and on this location link is having 5000 money. That will be assigned to pdr. So pdr basically in half of point is 2, this location that is 5000 money. And this loop will be connected. And if you have let's say 50 nodes and your position is 20, so loop will repeat that number of time and on 20 and over it will stop. Right? So this way we process this and now we can check if pdr equals to none. So it also happens. Like you are looking at the locations. So let's say position is 20 and you have to repeat those. So whenever you will be repeating this loop and pdr will be having null. So if pdr is having null, it means you entered the wrong position. So here you can write, no position does not exist. So this you need to check also and here pdr will end of loop. So step number 2, first loop will be tracking and then the set condition will be checking this. And if it is true, then this will be pretty good. Right? So here pdr will be having null. Okay. So here we need to check the order of your condition. So if available equals to null. So if available and this space is not there, if available is null. So you can say over to full step, if available and this space is there, then set new equals to away and away equals to link of away. Okay. So again, there will be new. It will be here. And it means this new is pointing to this location. Now this location is not under the control of available keys. So it is reserved and now we need to transfer this address in between this. Right? So I hope you understood till step number four. Now I am writing step number five, set info of new equals to item. So whatever values are available in this item, let's say it is an empty. So this 20 will go to info of new. So info of new is this part. So it will be having 20. Right? Now what we need to do, we need to store this address to link of pdr and whatever is available to link of pdr we need to store that here. Because when we insert this 20 in between 60 and 40. So after 20 we have 40. So this address is 1000. That should come here. So what we can do, first we need to store this address that is available to link of pdr to link of new. Right? Because if we store this address first, then 1001 will be here. So we need to store that first. So step number six will be set link of new equals to link of pdr. So link of pdr is 1000. It will be assigned to link of new. So link of new will be pointing to 1005. So now this number is pointing to this location. Right? And we need to remove this 1000 from here and we need to store 2000 so that after 60 we can go to 20 and then we can move 40. So for that we need to write step number seven and here we can write set link of pdr equals to new. Right? So new is having 2001. That will go to link of pdr. So link of pdr is this position. So it will be now 2001. So if I move this pdr from here, so what will happen? 6000 location next or link is having 5000. It will move to this. It is having 2001. So it is disconnected from here and it will move here because this location's base address is 2001 and its link part is having 1000 so far here. So this way you can see without moving values we have inserted one more in pdr. So this is an output. So this way I hope you understood all the algorithms. For that first and last and add up. And we can also convert all these algorithms with a specific programming address. So you can convert these algorithms with C, C++, Java or any other programming address. So if you want to watch like how we can write our functions based on these algorithms with the help of C language. So I also created those videos so you can go to description of this video. You will find links of the list like we are still using C. So you can watch those videos so that you can understand how actually in programming like this we can implement these algorithms. So I hope you understood whatever I explained in this video. Thank you for watching.