Deleting a node in Linked list | Data Structures Tutorial | Mr.Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this train was in the last session we discussed how to display all the elements in the single linkedlist how to insert a node in the middle of the linkedlist now in this session we will see how to delete a node how to delete a node in the linked list so remember if you want to delete a node from the linkedlist so first we have to check whether it is the first node or not or it is the last node or not or it is in the middle of the linked list right three different logics we have to write so why reasons see for example the function name we are writing delete delete' function we are writing delete and list also we are taking suppose root variable root variable so initially suppose is having only two nodes because in the first case I want to delete the first node first node in that how to write for example 20 null 10 and thousand is pointing to this location two nodes are they we know that root node is always pointing to that first node next how to delete element suppose I want to date the first node first we are checking so to perform the temporary operations we are declaring a pointer variable temp variable we know that root variable is a global variable no need to disturb the truth mean root variable so first we are checking what is the location what is the location so for suppose we are asking printf enter location to delete enter location to delete scanf percentage b address of location into location variable we are collecting elbow see just consider l Josie is a 1 the first node or for example if it is a 5 5 only a list is having two nodes but location is a fifth location there is no such location so we cannot perform deletion operation so clearly we are printing the misses if if the location what they entered is greater than length of the list simply length method we are calling length method or separately you can call and we can collect no problem right so if you want to collect a you can length and here it is simply we are pending nemesis invalid location invalid location the message we are giving message else if here we are writing else if imagine if the location is equals to 1 we are collecting into elbows E is equals to 1 nothing but the end user want to delete the first node from the single linkedlist write how to delete the first node so very simple first we are taking the help of one temporary variable right so here it is a temporary variable already declared into the temporary variable we are storing the root value so nothing but temp contains temp contains root root means what thousand clearly thousand is pointing to this one pointing if you want to delete the first node if you want to delete the first node very simple the second node right should point it by root after that after deleting the first node we should store the value 2000 into this location mm how to store 2000 mm we need to store into root variable root variable see this location of course root is pointing to the first node and a temp is also pointing to the first node but rule is all the operations we should perform with the help of temp variable only so don't disturb the root so temp alyou is a thousand thousand - link is a two thousand temp - temp - link value temp - link value is a two thousand will be stored into root here it is root so root value is not mm not thousand it is a two thousand it stop pointing and it start pointing to the second node first root is pointing to the second node now we have to delete the first node left-side connection is not there and we should remove a right side connection also reason suppose if node is connected to either from left side or from the right side we cannot delete that node nothing but we cannot release the memory which is occupied and one more thing very very important thing in any programming language no question of deletion of a memory only overriding possibilities are there we can replace the information we cannot delete the memory locations and we can we cannot delete the data which is inside the memory location manually so if you want to apply free function in dynamic memory allocation we have seen already if you want to release the memory free function we have to call but to call the free function right here it is the left side Corrections are not there right side also it should not supposed to connected to any other node so here you have to place a null value so that we can release this connection how to place in temp to link in a temp to link location we need to place the null value so here null value will be placed so connection also not there now this node is a free node is a free from the left side no one is pointing from the right side no one is pointing in the links so simply if you want to release right we have to free this temp variable free of temp variable free of temp variable right suppose if you want to delete the first node from the listing if you want to delete the first node from the list and finally a route should pointing to the second node so we have to delete the first node with the help of temp variable right simply like this right if the condition is a true if length is a greater than or equal to length it is invalid location else if if location is equals to one if it is the first location if you want to delete the first node so then what we have to do so means we need to execute this logic that will be deleted search suppose first location is okay in the middle or at the end right one more else block we have to write how it will delete observe how to delete a node which is in the middle of the single linkedlist or at the end of the single linked list for both only one logic is enough because I will execute and I will show you after return okay see suppose this is the link is the first node is the second node is a third node it's a fourth node so total four nodes I am taking 40 null suppose four thousand four thousand thirty third node link two thousand here second node link thousand and that will be stored into root root so it is ready again it is ready now how can we delete else--if else--if or simply else because for the middle or at the end this is enough if there is no such location if block executes if the location one else if block executes or if it is not the first location or location is not there then we can go for last else block just consider third node we have to delete if location equals to 3 consider imagine if location is a3 then how to delete if you want to delete this one first we need to send the control to here with the help of loop suppose we are declaring I value I value 1 while I is less than location -1 and one more variable we need to declare one more variable struct M struct node star like a pea variable is equals to initially root contains P it contains root what is the root value is it thousand initially root value we are storing into the pointer variable P of type of struct node type so it is holding the value thousand next I value starts with 1 1 is less than less than location - 1 location value is what 3 3 - 1 2 condition true so here inside that P - link value we are collecting into P already what is the p value thousand right to insert the element in the middle of the list how we are sending the control in the same way we are sending right but here it is wiser location - 1 there it is a location because there after we are inserting and here it is a in that case I one to delete that particular node I want to delete so before we should stop so minus one okay so here it is 1 minus 2 condition true P 2 link value will be stored into P P 2 link value is the mm mm next I value we are increasing but anyway I will you become 2 condition fail reason P is already pointing to this node P is already pointing to this node then one more pointer we are taking suppose Q Q we should make pointing to the node that what you want to delete suppose one more we are taking pointer to Q so here it is into Q what we are storing P - link value we are storing p value is 2000 p value is 2000 2002 link is what is a 3000 so 3000 we are storing into Q so Q value is a 3000 p-value is a 2000 now I want to delete this node how to delete if you want to delete this node observe if you want to delete this node first 4,000 you should store into this location 4,000 what is 4000 q2 link what is that q2 link is a 4000 q2 link will be stored into p2 link right we are storing into p2 link so 4000 will be stored here 4000 then it start pointing now we have to delete this one this connection already lost right side connection is there that we have to terminate so here in this link you have to place a null value so q2 link equals to null q2 link equals to null then here we have to place null value then right side connection also lost then simply what we have to do just we have to free of Q free of Q simple then you will launch this one this one sir in the middle okay suppose if it is the last node sir then how it you can apply the same logic I am executing observed see for example sir if location is a 4 consider if floatation is a 4 then how you will execute how you will execute for is nothing but a last node set then you said that same logic we can apply to the last node deletion also how just execute the same logic then you will understand clearly first of all first we are taking P root value root value is a thousand and location is a 4 and I value starts with a 1 so now 1 is less than 4 - 1 3 ptoo link value will be stored in two P P - link value is 2,000 X I value become - condition true next P - link value will be stored in two P is nothing but 3000 next value is a three condition Falls now then it will stop nothing but now P is pointing to this one third node next P - link value will be stored in to Q right so Q is pointing to this node pointing now observe Q - link value is a 4000 Q - link value will be stored in to P - link Q - link value Q - link is a null null value will be stored in - P - link this happens null then it stopped pointing next null value will be stored into Q - link null value will be stored into Q drooling already null so it is once again replaced with a null no problem or else right here it is you can remove this one if you write a separately but if you use the same logic then only this is the extra statement no problem now once again will be replaced with a null what will happen nothing instead of writing the code once again you can use this one and next finally free of Q then this will be deleted this is last node so for the first node only you have to write the logic separately if the node is in the middle or if the node is at the end only one logic is enough this is the logic is enough okay so this is how to delete a node so in all the previous sessions and in this session we discussed completely about how to append a node how to insert a node in the beginning and how to insert a node at the end how to insert the node in the middle how to delete how to display how to find the length okay almost all the operations basic operations on the linkedlist completed okay so in the next session we will see how to implement the code right for all these operations in a linkedlist right I hope you enjoy this video for more videos please log into not hid channel thank you
Info
Channel: Naresh i Technologies
Views: 261,669
Rating: undefined out of 5
Keywords: Data Structures, Srinivas, Naresh IT, Hands on Data Structures Training, Data Structures Demo, Online Data Structures Training, Data Structures Tutorial Videos, Data Structures Overview, Data Structures Interview Questions
Id: qIjIwHiwkBg
Channel Id: undefined
Length: 17min 7sec (1027 seconds)
Published: Wed Sep 28 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.