 Hello friends, I am Sanjay Gupta, I welcome you to Sanjay Gupta Tech School. In this video I am going to explain how to write algorithms to delete an item from Central India. So we will be discussing three different algorithms. First we will be deleting first element from the list. Second algorithm for deleting last element from the list. And third will be deleting a particular location or particular node from the list. So first I am going to explain this first algorithm that is for deleting first node from the list. So let's start. So name of the algorithm is delete first. It will be having these parameters. So start is the point that holds address of the first node. So this is our first node, this is second node. And we can have any number of nodes in our list. And each node is divided into two parts. First is known as info or value. Second is known as next or link. So here you can see I am mentioning these as well. So info will be pointed where value is stored in the node. And link will be pointed where address of next or node will be stored. And collectively this is known as a node. And its base address is always a reference to start. So start is containing base address of this first node. This first node link part is having base address of second node. And this way you can connect any number of nodes into a link. So now we are going to delete this first node from the link list. And this will be the fourth variable that is item. So it will hold the value or can store that is stored in the node that we are going to delete. So let's say if we are deleting first node. So item will be storing value as step. So now I am going to write step number one. So step number one will be for checking under flow condition. So if part equals to none. So if your start is in null, it means there is no node available in the link list. And if node is not available then we cannot delete any element of node. So in that case you can write a message saying that link list is empty. And then you can write exit. And I am going to delete these so that we can utilize the space. So if your start is null, it means your link list is empty. So you can print this message and you can exit your avatar. And why I implemented this here? Because whenever you will be implementing this avatar with any programming language, like C, C++ or any other programming language. So in that case you need to check your starting pointer. Whether it is null, it means you cannot apply deletion operation. So that unnecessarily you can avoid execution of the statements. Now if this condition is false, then you can apply step number two. So in case of step number two, I am writing set LOC equals to start. So LOC is also pointer. So if link list is not empty, it means we have various nodes available in the link list. So now we are going to delete this false node. So its address will be at the end of this. So let's say this is LOC and it will be at address as 1001. So both start and LOC point that both are pointing to this point. Now what we need to do? We need to remove this 1001 from here and this start pointer should point to this second node. So that this node will be removed from the link list and we can delete that particular memory location so that this false node will be deleted from the cellular interface. So now after assigning this, I am writing step number three. So step number three says there are two scenarios. First scenario says we have only one node available in the link list and second scenario says we have more than one node. So let's say for example, this is start and this is the node. So now we have two scenarios. So one scenario is this where we have only one node and another is this where we have two nodes. So if we have only one node and we want to delete this node, so what will happen? Again start will be done. We need to store null in the start because this node will be deleted. Now we need to recognize like how we can identify link list having only one node or more than one node. So we need to check this link of start. So start is pointing to this 1001 address and if its link part is null, it means your link list is containing only one node. So here you can write if link of start equals to null. So start is pointing to this node and if its link is null, it means your link list having only one node. Now let's apply this condition of this link list. So link of start is this and it is having 2001 which is not null. It means this link list is having more than one node. So now we are going to write separate statements for both the scenarios. So if this condition is null then set item equals to info of start. So start is pointing to this first node. So its information will be available into item and we can write set start equals to null. So we already know we have only one node in the link list and then here we need to delete. So our start will be having null after that. Now else part. So if this condition is false, it means this is the scenario. We have multiple nodes available in the link here. So what we need to do again set item equals to info of start. So info of start is 10 that will be available into item. Now we want to delete this node. We want to delete this node. So what we need to do if you want to delete this node, it means this node's address should be available in the start and this node's address is available here. And this location is known as link of info. Sorry, link of start. This location is known as link of start. So we can assign link of start into start. So what we can write set equals to link of start. So link of start is 2100 that will be stored in here. So now start is not pointing to this location. Instead it is pointing to this. So this way you can see this node is deleted and start is pointing to this node. And this node's address is available into LSE because that is already available here in step number two. So here we can write end of this then step number four. So now in case of step number four, this LSE pointer is containing address of the node which we are going to delete from the link list. So we disconnected that node from the link list but physically it is still occupied. So now you need to write a statement so that this memory will be released and this can be used by any other program. So here you can write free IOC and then you can write end. So whenever you will be implementing this IOC I think programming language. So in each programming language the method to destroy a memory will be different. So if you are using C, so you will be using a function free. If you are using C++ then you will be using the keyword B. And in case of Java, the unoccupied or unused memories will be automatically through garbage collector. So in each programming language the process of building a particular location is different. So that's why I mentioned the IOC or the IOC. So that can be occupied by any other program. So this is the algorithm that is for deleting first element from a single case. So I hope you understood this. Now we are going to understand delete last. So let me erase this one first. So now we are going to understand algorithm for delete last. So in case of delete last, we need to delete the last node from the link list. So here you can see name of algorithm will be delete last. Then it will be having these. And we just start implementing that IOC string in case of delete first. Then step 1 will be as it is. Because if start and run then we don't need to execute any other statement. So we can just just delete this and exit. Now step number 2. So in case of step number 2, we need to check whether link list is having more than one node or more than one node. Like we did in case of delete first. So how we can identify that we need to check the link of start. So this is the link position. This is info. So we need to check link of start because start is pointing to first node. So we need to check the first node link part. It means we have one node otherwise we have more than one node. So here I am writing if link of start equals to none. So if this condition is true, it means we have only one node. So here we can write set IOC equals to start. So IOC is a pointer that will be holding the address of the node. So if we have only one node in that case, that node address will be available in code. But if we have more than one node, so in that case the last node address should be available in code. So this is the case where we have only one node. So IOC will be containing the address which is available in code. So if we want to start, then in B we can write set item equals to info of and in C we can write start equals to none. So these three statements are responsible where the link is just having only one node. Now what if this condition is false? It means our link list is having more than one nodes. So let's say if this is the case. So what we need to do, we need to reference the last location and we need to remove this node from the link list and its previous nodes link part should be null. Its previous nodes link part should be null. So now we need to start a loop because we don't know how many nodes are available in the link list. So we need to start a loop. So here I am writing set PTR equals to start. So PTR is a pointer that will be holding address of costume. Now I am writing a loop, repeat steps while link of PTR not equals to null. While link of PTR is not equals to null. So let's say this is step number A, this is step number B. Now this will be in certain steps. So initially PTR is pointing to start. So PTR will be here. So we need to check link of PTR, so link of PTR is not null. So PTR will move to this location. PTR will move to this location. Now in this case PTR link is null. So here our loop will be terminated. So we have last nodes addressing to PTR. But we don't have address of its previous node. And we need that because PTR is pointing to last node. But its previous nodes link part should be null if we want to delete this node. So for that purpose we need one more pointer. So I am writing P. So P will be the pointer which will be pointing to the previous location of the PTR. So here I am writing P equals to PTR. And then PTR equals to link of PTR. And this is end of loop. This is end of loop. So now let's understand this thing from the beginning. So PTR is pointing to start. So PTR will be here. Then check link of PTR. It is not null. So it is true. So PTR will be assigned to P. So this location will be pointed up to P. And PTR will be moving to link of PTR. So PTR will be moving to PTR. Now again we need to check this condition. So PTR link is null. So this loop will be terminated. So here you can see PTR is pointing to last location. And P is pointing to its previous location. And if we have more than two nodes. So then this loop will iterate again and again. And this condition is true. So it will be repeating. And PTR will be pointing to last node. And P will be pointing to the node that is previous to that PTR. So now we have both the pointer P and PTR. So we need to delete PTR. And we need to set link of P as null. So here I am writing that null firstly. Set item equals to info of P. Sorry info of PTR. Because we need to delete this node. So PTR info that is 20 that will be stored into PTR. So item is containing the value which is available in the node which is pointed to PTR. And LOC equals to PTR. So now this LOC will be pointing to 2001. That we are going to delete. This was the case where only one node was affected. And this is the case where we want to delete the last node. Because we have more than one node. So LOC will be pointing to this node. So we expected info of PTR. We expected address of this node. Now we need to delete this from this linkless node. So what we need to do? We need to assign null here. So for that we can write next step. Set link of P equals to null. So link of P is pointing to this node. And its link part will be null. So it means if this node is null. So this node is not connected with this. So it is removed and its address is available into LOC. So now here you can write N of S and step number 3. Free LOC and step number 4 will be N. So now you can see this LOC is pointing to this location. So you can raise this location so that any other program can be using this location for storing some other node. So this way this part is for deleting node from the linkless. If linkless is having only one node. And this is the process where linkless is having more than one node. And we are going to delete the last node from the linkless. So I hope you understood the delete last as well. So now I am going to delete this so that we can understand delete after. So now we are going to discuss delete after. So here you can see I have created a new mapping map for the linkless. So start is having 1001 which is first node. First node is having address as 2100 which is heading to the next node. So they are being done already created. And here we have one more option address position that we are going to delete. So delete after works based on the position that you want to delete. So in case of delete first it is specified we need to delete the first node. In case of delete last it is specified we need to delete the last position. But in case of delete after we need to specify which position you are going to delete. So let's say we want to delete the second node from the linkless. So again this first I am going to say if start is null then linkless is empty and we can display the message linkless is empty and etc. Now step number 2. So here I am writing set i equals to 1 and pdr equals to start. So if linkless is not empty it means we can create the linkless so that we can move to that node that we are going to delete. So here i equals to start. Now step number 3. So here I am going to implement a loop. So repeat while i less than position. So if value of i is less than position then this loop will be limited. So here I am writing set pdr equals to link of pdr. So pdr will be pointing to the next possible load and i equals to i plus 1. So I will be limited by 1 and here I am writing end of loop. So position is 2, i is 1, i is less than position so it is true. Initially pdr is pointing to this pass node. Then if this partition is true then pdr will point to next node. So pdr is now pointing to this node that we are going to delete and i will be limited by 1. So i is 2, position is 2 so this loop will be limited. So now we are on this node that we are going to delete. Now if, so here we need to check link of pdr and if link of pdr is equal to null. So here we can write. So here we can write. So here we can print the passes null node delete and we can write exit. And one thing I want to correct so position is 2 and we are working on delete after. So we are not going to delete this node. We are going to delete the node which is after this position. So this I am going to correct. So I realize that we need to delete this but this algorithm is limited to add after. So we are on the second position we are going to delete the third. So the node which is available on after the second loop. So that's why we check this condition if link of pdr is null. So pdr is pointing to this node its link is null. So it means it is having one more node that we can delete. Let's say position is 3 so pdr will do on this and its link part is null. It means we don't have any other node after this node. So delete will not take place. So that's why we check this condition. So position here means we need to delete the node which is available after that position. So mistakenly I told we need to delete this but we need to delete the node which is after this function. So until here we have done. Now we need to write the stack number 5. So I can write said noc equals to link of pdr. So we are going to delete this node. So I am assigning link of pdr so pdr is pointing to this node and its link part is having 3,000 nodes that will be stolen into LOC. So this is LOC pointing at between the point 0 to 3,000. Now what we need to do? We need to assign this null from here to here. We need to assign this null from here to here. And maybe we have more nodes so whatever values are involved at this position we need to assign that position a value here. And before that one moves that item equals to info of So now we need to expect this value. So pdr is pointing to, link of pdr is pointing to this location and we need to identify this information. So what we can write info of link of pdr. So first we pick pdr then we will move to its link and then through that we will move to its information part. So that will be stolen into item. So this way we accept the item. Now what we need to do? We need to set this value from here to here. We need to set this value from here to here. So that this node will be removed from the pages. So now set number 7, set link of pdr. So where we need to store the value in link of pdr, right? Which value this one? So how we can reach here? Just like we did this info of link of pdr. So this way we need to expect this link of link of pdr. So link of link of pdr. So pdr is pointing to this location. It's linked as 3000 and 3000 is linked as null. So that will be assigned to this location. And then you can write 3, NOC and then answer. So this way this is the algorithm for deleting after this position. So I hope you understood how we can find all three algorithms. Those are related to delete first location of the link list, delete last location of the link list and delete a location that is after a particular link. So this way I hope you understood all three algorithms. And if you want to watch the programs implemented in C related to these algorithms, so you can go to the description of this video where you will find links of links. And you can follow them. Thank you for watching this video.