DOUBLE LINKED LIST (CREATE AND DISPLAY) USING PYTHON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our session so in today's session we'll see about the double linked list so how can we create the double linked list and how can we display the nodes of a double linked list so in the further sessions we will cover about the insertion and deletion concept so let's start the session about a double linked list so in the previous session we have covered about the single linked list where every node will be having two fields one is data and another one is a reference to the next node but here in the name itself indicates as a double linked list every node will be having two links one is for the previous node and one is for the next node so if you consider a node so let us consider this as a node okay so it will be having three fields this is the actual data this is the actual data and this is the previous which is a link to previous node previous node the address of previous node and this is the next and this is the next that is next node address the address of next next node okay so here it refers the address of previous node and this is the address of next next node so every node will be having these three fields so we have to create a node with three uh default parameters right so within three variables one is for data another one is for previous another one is for next so if it is a first node if it is a first node automatically there will be a data and the previous is none because there is no previous node and here it is a none which is the next is also a none right so this is the first one first node if it is a second node if you insert a one more node if you insert one more node for example let it be some 10 let it be some 10 right so n1 is a 10 so this is the first node so there will be no reference to the previous node and there is no reference to the next one so that's why we have kept as in none so previous and next test and now and if you consider if you create a one more node that is n2 so here what the 20 is the data now 20 is the data is the second node and this node previous should be address of n1 okay this node address should be previous i mean this previous should be address of previous node so what is the address of previous node let it be some thousand and here we need to give the thousand here and here in the first node the next reference to be n2 let it be some 1100 so 1100 should be placed here right that means n2 dot previous should be n1 and n1 dot next should be n2 n1 dot next should be n2 n2 dot previous should be n1 and this will be automatically none this will be automatically none if you insert the third node if you insert the third node then the third node address should be placed here so that's why n3 sorry n2 dot next n2 dot next will be n3 and n3 dot previous is equal to n2 so like this we have to create the multiple nodes right so let's see i will give you a small overview and then we'll move with the program i will demonstrate the program first we need to create a node so in order to create a node we go with a class node so similar to our single linked list so here i am just defining three variables i am initializing three variables so i am writing some constructor here so init so self and data self and data right and so self dot data will be data and self dot next will be none so initially self dot previous is equal to none so this is the creation of node this is a creation of node if you create an object for this class automatically the data will be placed in self.data and the remaining will be none and then we need to give the links right so after this one we need to create one double linked list so that we have to insert the data into that particular double linked list so for that purpose i will go with one more class called class dll and here i will give the instructor so init and i will initialize the head so we know that head is a pointer pointing towards the first load so head will always point to the first node now here initially it will be none because there are no elements there are no elements so that's why i have placed here none right now now as i have said that if you create n1 for n1 so this is the object creation for node for example it's 10 now n1 is 10 automatically this will be created so n1 sorry a node will be created here i will erase this one i will just give the handling right so a node will be created with a 10 and here it is none and here it is none because we have already seen the class of node right so we have defined the data this parameter to the data and the previous and next will be initialized to none so this after executing this statement a node will be created like this then this is the first node okay this is the first node so we need to initialize it l dot so l will be the object from double increment so l dot head l dot head is equal to n one so automatically it will be pointing you as head right next if you create n2 n2 is equal to node 20 so one more node is created here so obviously it will be 20 and it is none and it is none based upon the class class node okay now we need to link here we need to link here so how to link so the address of this one the address of second node should be placed at header dot next right so here n one dot next is equal to that means this position is equal to this is n2 right so this is n2 and this is n1 so n2 automatically immediately it replaced none with some 1100 1100 and next here in this position okay a link is established again a previous okay here for node 2 the previous should be linked to the previous node that's why n2 dot previous so obviously this is a n2 dot previous and this is n2 dot next so n2 dot previous is equal to what adder should be here the n1 address should be here so n1 so automatically none will be replaced with so let it be the address is thousand so thousand will be placed here automatically one more link is established here right so this is called a double liquid list double linked list so every node will be having the link to previous node and the next one and again if you create n3 n3 is equal to node of 30 so one more node will be created here see and one more node will be created here so 30 and this is none and also this is none right now we need to establish the connection between these second node and the third node so this is a n3 now n2 dot next so this should not be none right this should not be none so n3 dot next is equal to sorry n2 dot next n2 dot next is equal to n3 automatically none will be replaced with the address of node that let it be uh some 1200 1200 so automatically a link is established to here and now n3 dot previous n3 dot previous it should be n2 so there must be a link between n3 and n2 so this will be replaced with 1100 and automatically it will be none right so this is how we can create the double link release double linked list okay now how to display it so in order to display first we will see whether the double linked list is empty or not so how to know that so if self dot head because immediately after inserting one node we are we are assigning the header to that particular first node so self.head is none automatically we can say empty double linked list yes if it is not none if it is not none that means there are some nodes so just in order to travel from the first node to the last node just assign some uh assign this head to some temporary variable so because we should not move the head right self not head now we can move the temporary so while 10 while tim that means unless the tempo is having the value the loop will be repeated right so what we have to do just we need to print temp dot data okay so just uh for representation i am taking all these parts right temp dot data and immediately after displaying that we need to move the temp to next one so temp is equal to temp dot next so automatically first it will be displayed and immediately temp dot next means it will move to the 1100 this position so again it will check for the value so yes it is having the value so this value will be displayed and again temp is equal to 10 dot next so here temporary six 1200 it will be moving here so again here 30 is a value 30 will be displayed again temp is equal to temp dot next so here none so automatically here none means so this condition fails a temp is not having the value that means false so automatically it will come out from the loop and it will display as 10 20 and 30 right so complete you keep all these things in the function called display display function right inside the display function we can write all these code right so hope you understood this one double linked list so creation of double linked list and display the nodes of a double linked list so here every node will be having three fields one is a data one is a reference to the previous node another one is a reference to the next node right so in order to display we need to check whether the double linked list is empty otherwise we need to travel from the first node to the last node by using some simple while loop right so hope you understood this one so let us demonstrate these two functions that means i create and display by executing a small program so let's move on to the system hello friends so just now we have seen the about the double linked list so how can we create the nodes and how can we display the nodes of a double linked list so in the previous session we have seen about the single linked list the similar way we will create a node but it here we are having an extra variable called previous so because it is a double linked list every node will be having the previous link and the next link right so let's demonstrate the create and display so first of all create a node so in this node i will go with the constructor so in this constructor i am defining the three parameters so three variables right so one is self dot data obviously the second one is self.previous so for every node there will be three different fields it's previous data and next so previous is the address of the previous node and next we'll having the address of the next node so initially i will go with the none for previous and also next for none right now i will create one more class for double linked list and here i am creating one constructor server head so self so here and taking some self dot head is equal to none so initially there is no elements so no nodes so that's why none if you insert any node the self dot head will be pointing towards the first element right now we'll create the linker list object for link double linked list right now we will write the display function we'll write the display function in order to display all the nodes right so display and if if self dot head is none that implies it's an empty double linked list empty double linked list right so here you have done here and if you call this display function display function will get empty double linked list because we are not creating any node right and now after that i will create one node n1 is equal to node of 10 10 and this should be initialized to head right so self dot head is equal to n1 and obviously n1 dot previous because this is the first element n1 dot previous must be empty and n2 dot previous n1 dot next should also be none so just will display the thing so here we have to write the else part because now self dot head is not a none right self dot head is not none now just take the temp variable and assign the head to temp so that we need to travel the temp from first element to the last element that means first node to last node so just y 10 so it this loop ends whenever the temp becomes null okay so unless the temp temp is having the value automatically it will be moving on so just print the data so we are having some temp dot data and give the representation and also display each and every node in the single line and move the temp to the next one temp dot next right so sorry here it is a ll l is a linked list right so l dot n so only one element is displayed here hope you understood this one only one element is inserted so one element has been displayed now if you go with the second element n2 is equal to node of 20 and then so now node 2 is there n2 dot previous must be n1 and n1 dot next should be n2 so for the first node the next element will be n2 and for the current node the previous should be previous so if you execute this one will get up to second value so 1020 and if you go with the third value n3 is equal to node of 30 and similarly the third node so n3 dot previous will be n2 and n2 dot next value will be n3 similarly n4 is equal to node of 40 similarly n4 dot previous will be n3 and n3 dot next will be n4 so if you execute this one we are getting some nodes 10 20 30 40. so for 20 the previous will be 10 the next will be 30 for 30 the previous will be 20 the next will be 40. for 40 that previous will be 30 and the next will be none right so this is how we can create and display so in the next session we will see the insertion so inserting a node at the beginning or end or in the specified position right so let's stop here hope you understood how to create the double linked list and how to display the notes of a double linked list right so if you are having any doubts regarding this create and display feel free to post your doubts in the comment section definitely i will try to clarify all your doubts and if you real 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 very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 2,939
Rating: 4.9148936 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, structures, interview, placements, data structures in python, oops in python, classes, objects, data structures and algorithms, creating linked list, display, head node, links, single linked list, sandeep, double linked list, prev node, next node, data, oops concepts, class
Id: 89Gma338jHU
Channel Id: undefined
Length: 18min 54sec (1134 seconds)
Published: Wed Feb 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.