DOUBLE LINKED LIST (INSERTION AT BEGINNING,ENDING AND SPECIFIED POSITION) USING PYTHON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our channel so in today's session we'll discuss about the insertion operation in double linked list so in the previous session we have seen how to create the double linked list and how we will display all the nodes of double linked list now in this session we will see the insertion operation double linked list right so first let us consider some elements then we will apply the insertion operation so how we can insert the element or a node at the beginning or at the end or in the specific position right so first let us take four nodes and then we will see so name itself indicates every node will be having two references one is to the previous node and another is to be the next node so in the previous session we have already seen how to create that node right so we have taken some three variables in the node class right so this is n1 n2 n3 and n4 let it be 10 20 30 and 40. the address is some thousand some two thousand and the three thousand and the four thousand now this is none because this is the first element there is no previous this is none and it stores the address of next one two thousand and this is thousand three thousand and again two thousand and four thousand and here three thousand and none because this is the last statement right so there will be two links right now so let us let us assume these are the four elements now so we need to insert the element first at the beginning insert at beginning so if you insert the element at the beginning so this will be the head right head this will be the head now if you insert the node at the beginning what we have to do so in order to insert the node the n is equal to some node of five so if you give this one automatically one node will be created here with 5 and this is a none and this is not okay so initially none and 5. now we need to link these two so if you insert this node and in the first one we need to change the head we need to change the head so before changing the head we need to change the we need to make the link between the first node and the second one so this becomes the second node right now see let us take some temporary variable and initialize the head to temporary variable so the head will not be changed now see then after executing this one this becomes the temp this becomes temp now temp dot previous temp dot previous so what is the value of temp not previous that means this position this position must be this one right this position must be this one so sorry yeah n yeah so automatically let it be some 500 okay 500 so here none will be replaced with 500 automatically a link will be established from n1 and that means our present head to new node now we need to change this one so we because we have to produce a link between first node and second node also now n dot next n dot next which will be temporary so automatically this none value will be replaced with 1000 right so link will be generated now we need to change the head value so head position should be changed to here head position should be changed to here so self dot head is equal to this value that means n because n is a node right hope you understood only few steps so first we are creating one node and we need to insert that node at the starting position so after inserting the node this will becomes like this with two references as none so we need to establish a link between the first node i mean the previous first node and the current node so for that purpose we are using these two steps and after that we are just changing the head position to the current node right so hope you understood this one insert and beginning right now so i will demonstrate this by executing a program don't worry the next one is insert at end at the end we need to insert it right so let us take some n is equal to a node of so it's 40 so i will take 50. so i need to insert some 50 element at the end so after executing the statement automatically a node will be created a node will be created with 50 with both the non values both reference has none so we need to link up with the last element to the present last element with the new node because new node will be the last statement now right for that purpose we need to travel from first element to the last element so in order to travel just i am taking some temporary variable and i am initializing the head value to the temporary variable and i am just traveling from first element to the last segment so while temp dot next temp dot next temp dot next means see so after insertion the head so this is the head draw right and also temp now temp dot next is not none so unless the temp dot next is none it will repeat the loop so what we have to do we have to change the temp is equal to temp dot next see temp dot next is not none true so automatically temp will be temp dot next so temp value will be at thousand now now again temp dot next is not none right again temp will be moved to here again temp is equal to temp dot next so temp dot next is not none again change the temp to temp dot next so 3000 so here it will be so like this it will travel from first element to last element now the temporary variable is at here right and this is nothing but n so new node now what we have to do we need to change this one we need to establish a link between the temporary variable i mean present temp value to the new node for that purpose go with the temp dot next temp dot next is equal to here we need to give the address of n so let it be some 5000 right so temp dot next is equal to n so automatically none will be replaced with 5000 so automatically a link is established here again the new node previous should also be changing because for every node we have to be linked with the previous node and the next node so obviously n is the last element so n dot next will be none and n dot previous will be the three address of previous one so for that purpose n dot previous is equal to so replace this with this value temp so that means four thousand right four thousand so this is how we can insert the element at the end right so first we are traveling from first element to last element and then we are establishing the link between the temp and the new node right for every node there must be a reference to the previous node and the next node so if you draw this diagram these lines will be very easy to write okay unless you write you draw the diagram so it is somewhat difficult to write the nodes right so you have to imagine if you draw this diagram or if you draw these nodes it would be very simple to write these steps right hope you understood this insertion at beginning and insertion it ending now we'll go with the third case that is insertion at specific position we need to insert the element at specific position right now so i need to insert the position at one two three four and data will be fourth position that is the 25 i need to add 25 right position 4 and 25 i will write here position is equal to 4 1 2 3 4 and data is equal to 25 so automatically first we need to so i will write the heading okay insert at position now take the data node off let it be some four let it be some four so just we need to change the temporary variable okay sorry we need to travel from first to node to just position minus 1 that means 3 up to 3 it should be traveled so now what we have to do so take the temporary variable and assign the head position so automatically now this is the m now what we have to do we have to travel so for i in range 1 2 position minus 1 1 2 position -1 so automatically change the temporary variable temp is equal to temp dot next so i ranges from 1 to position minus 1 means 3 that means i is equal to 1 i is equal to 2. right so after first iteration the temp becomes here right because first iteration temp is equal to 10 dot next temp is equal to 10. means 10 000 so it points to the thousand so here the temp will be at thousand now here again where i is equal to two within the range so automatically the temp is equal to temp dot next temp is equal to m dot next means 2 000 so here the temp will be pointing so now the temp will be at 2 000 and i is equal to 3 it doesn't happen because the range of 1 comma 3 means it will iterate up to 1 and 2. so now the temp value is here now the temp value is here and a node is created here a node has been created here with a 25 and this is a none and this is a num so we need to link these two we need to link these two right so now n dot previous is equal to n dot previous means this one so this is the previous and this is the next right so n dot previous is equal to what we have to get here this value okay this value temporary okay because we are inserting this element here so we are just removing this link and we are inserting the link here right so here n dot previous this is the n value n dot previous must be this one 2000 right what about n dot next this one three thousand three thousand means tempo dot next temp dot next so automatically we'll get three thousand so is this sufficient no because again this stem value is pointing at 3000 so we need to change this and also this one right so we need to change this one we need to change this one so how to get this position how to get this position simply temp dot next temp dot next means 3000 okay this this this field dot previous automatically this one and what is the address here it should be new node that is uh let it be 2500 so this 2500 should be here that means n okay so i hope you understood this one because we need to link here and this two here so automatically n address should be placed at previous node right so to get this one temp dot next that means three thousand dot previous this position so we are changing to n and what about this one this should be changed three thousand should be changed to address of this one two thousand five hundred right that means temp dot next is equal to two thousand five hundred bits again n so this is the code we need to write in order to insert the value at particular position right so this is very easy if you draw this example if you draw some nodes it would be very easy to write the content okay so first we need to insert the node we have created one node so then we have traveled from first element to the position minus one so that we need to establish a link between the position minus one to new node and new node to next mode so for that purpose we are taking some n dot previous and n dot next so n dot previous it should be the temporary and n dot next should be the temp dot next so because initially the temp dot next is here itself right so then we need to change the positions of temp dot next and uh the next node dot previous so for that purpose i am taking temp dot next dot previous is equal to n so that it will be changed and tem dot next equal to n this will be changed so if you write this statement in the first line automatically again the code will be different we need not write this one so this this statement will be wrong so just you need to follow the order so what we have to do first and what we have to do next so that should be important right so hope you understood this one so this is how we can insert the node at end beginning and specific position right so now i will execute a program demonstrating all these three functions inserting a node how to insert a node at the end beginning ending and specific position right so let's move on to the system hello friends so just now we have seen how can we insert the nodes into a double linked list so in the previous session we have covered how to create the double linked list and how can we display the nodes of double linked so now in this session we will see the demonstration of insertion method so in order to reduce the time i just copy the code so in the previous session we have written some code for creation and display i will copy the same code here and directly will move with the insertion right see this is the code which we have written in the previous session so i have we have inserted some five nodes so if you execute this one we'll get some five nodes 10 20 30 40 and 50. so as the name it itself indicates it's a double double linked list so every node will be have pointing to the previous node and the next node so every node will be having the reference to the previous node at the next node so now we'll write the insertion so first we'll see the insertion at the beginning right so insertion at the beginning so i will write the method define insert underscore beginning right so we need to pass the data here so in after that create a node so n is equal to some node of data so now the node will be created with both the references none that means here uh if you after executing this one n dot data will be data n dot previous will be none and dot next will be none now we have to link up so we are inserting this node at the beginning itself that means we need to change the self dot head so initially the first element will be having pointing to the self dot head so now the self dot head should be changed to this new node because this node is inserting at the beginning for that just will take a temporary variable and we'll assign the self dot head and then next we need to change give the links so temp dot previous so temp is nothing but our second node after inserting the first node this new node the temp will be pointing towards the first second node because temp is equal to self dot head so temp dot previous so what what about the reference to the second node it must be a first node now first node is n right and similarly n dot next so current node dot next should be 10 right and change the self dot head change the self dot head to en so simple steps it's a simple steps so we are just creating a node so with the two references with none so the second node if you insert this as a first node the second node must be i mean current uh head must be pointing towards the second node so we need to change the head so that's why we are giving some temp value for the head position so temp dot previous must be the first node that is n and n dot next should be the second element that is the temp and self dot head is equal to none so we'll create the node right so after that l dot insert underscore beginning of let it be some 5 5 execute so you can observe here 5 is appended here right so again if you execute the same method once again with a new data let it be 7 execute so 7 will be inserted at the beginning right if you execute 5 7 10 20 30 40 and 50. so this is how we can insert at the beginning now what about the ending so how to insert the element or a node at the ending so write down the function so self comma data so we need to create the node so here also i will go with the n n dot node of data so last so we need to travel from first element to last element for that i am taking the temporary variable and i am just assigning the self dot head to temp and i'm just giving the loop to travel from first node to the last node so while temp dot next is not none so automatically after executing this one so we need keep on changing the temp right temp dot next after executing this loop so whenever the loop comes i mean the control comes out from the loop the temp will be pointing towards the last element now we need to change the last element next to new node because the last element next will be pointing towards num because there will be that is the last element now here we are inserting a new node at the end so that means the temp dot next so whenever it comes out from the loop it is the last element so temp dot next will be pointing towards n and automatically n dot previous must be temp end of previous must be 10. so we are moving from first element to the last element and we are changing the references now we will see we will see i will give some here and let us take l dot insert underscore sorry end 60 now after executing that we need to get a 60 as the last element see here 60 is the last element so before applying this line the output will be 10 20 30 40 50 and here whenever you are inserting some element at the end automatically the next element will be pointing at 16 right and again if you poi if you insert one more element that will also be pointing at last that means that will be added at end element right 70 so you can observe 10 20 30 40 50 60 and 17 right so hope you understood this one beginning end and it's a very simple methods next means if you want to insert the element at particular position what we need to do so we need to pass the position and the data position and data so create a new node so that node should be inserted at the given position so in the position first we need to travel from first element to the position value position but one so for that we'll assign some head value to temp self dot head right now we need to go from first element to the position so for that for i in range of one comma position minus 1 and keep on change the temporary variable so up to the position minus 1 it will be pointing now just change the previous and dot previous n dot previous is equal to temporary and similarly n dot next is equal to temp dot next and then temp dot next dot previous so in before changing the temp value we need to assign the temp dot next dot previous element to the n because we are inserting the new node in between the different nodes right so that's why just go with this one and next temp dot next is equal to n now you can observe here so let us put the commit here for these two lines so l dot insert underscore position uh let it be 3 comma 25 and if you execute this one you can observe 10 20 25 so at the third position we are getting some 25 the value 25 right so at the third position we are getting the value 25 hope you understood this one right so in order to change the i mean in order to insert the element at required position we need to change the head that means we are assigning some head value to the temporary variable and we are just keep on moving the temporary variable to the position -1 and there we need to give the reference for previous and next and then we have to change the value of temporary right so hope you understood this one and again one more uh method we'll call i will show you insert position let us take fifth position so one two three four fifth position with a 35 execute this one so you'll get fifth position the 35 will be inserted so 10 20 25 30 35 40 and 50 right so hope you understood these three functions insert at the beginning inserted and inserted the specific position right so let's stop here in the next session we'll go with the deletion operation so if you are having any doubts regarding this insertion either at the beginning ending or a specific position so feel free to post your doubts in the comment section definitely i will try to clarify all your doubts and if you really understood my session like my session share my session with your friends and don't forget to subscribe to our channel thanks for watching thank you
Info
Channel: Sundeep Saradhi Kanthety
Views: 2,528
Rating: 4.9036145 out of 5
Keywords: sundeep, saradhi, kanthety, data structures, arrays, lists, stacks, queues, trees, graphs, primitive, non primitive, linear, non linear, linked lists, ds fundamentals, ds basics, nodes, self referential structures, list creation, cse, data structures in python, oops in python, classes, objects, data structures and algorithms, creating linked list, display, head node, links, single linked list, sandeep, insertion, begining, ending, specified position, implementation, double linked list, dll
Id: JeS4povWMAQ
Channel Id: undefined
Length: 26min 45sec (1605 seconds)
Published: Thu Feb 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.