Double Linked List in Data Structures and Algorithms | Part-2 | by Mr. Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is cinemas in the last session we discussed how to append a node in a double linkedlist so now in this session we will see how to add a node at the beginning of the list and how to find the length of AB a linkedlist and how to display all the elements in the double linkedlist all the three we'll see so first one how to add a node in the beginning of the list second one how to find the length of the double linkedlist and how to display all the elements in the double linked list see suppose some of the nodes I am taking this is one this is a second node this is a third node already list is having some of the nodes suppose here it is a 3000 this node address is a 2,000 this node address is a thousand root variable always pointing to first node pointing to first node and next the data we are taking 10 first node left link is a null and right link is a next node is there so mm we are storing here and previous node address thousand we are storing here data is a 20 and next node address 3000 here previous node address mm here data is a 30 so toolings are they and now here it is a last node right link will be null so this is a structure of a double linkedlist now concept is how to add a node at the beginning add add at begin any function you can write to understand easily I am writing void suppose if they call this function add at begin added begin and here we are writing because we need to create node also so here we are writing wide and at begin if they call first we have to create the node we know that all the operations in the list we need to perform with the help of a temporary variable only temporary variable so first we are declaring a variable struct node star temp variable we are creating so temp gets memory allocation now we need to allocate the memory sir how to allocate the memory with the help of a malloc function we know that so temp equals to here it is struct node star typecasting ml log function and it is taking sizeof struct node sizeof struct node this is calling we know the so many times we have discussed so here what it will do it will create a node it will create a node at a particular location suppose location is a 4,000 so that will be stored into temporary variable so temporarily this variable pointing to this one and next one so we need to read the node data right here we need to store the data and here it is a left link is a null and the right link is a null suppose here we are asking printf enter node data enter node data next one scanf scanf here it is percentage d how to read the information in to this location this location address we need to provide suppose this is a data location consider data m to data address that is a location address so here m to data address we are providing temptu data address we are providing so this is next m2 left link is a null into the tab temp - left link we are storing null value and into temp - right link also we are storing the null value m2 right link is also null temp - left is equals to null and temp - right equals to null so here it is a no Deezer here null and here also null so as of now node is ready suppose node data is a 40 node is ready now connections very very important so we need to establish it three connections so what are the three connections observe suppose if it is a first node means what list is not having any node so what you have created is the first node consider so by that time so directly route should pointing to the newly created node that is very easy so how can we understand right whether the list contains nodes are not simple if no nodes in the list root value will be null so here we are writing like this if root value equals to null value if root equals to null means what no nodes in the list no nodes in the list so by the time root should point into a newly created node so what is that newly created node is a 4000 which is already inside the temporary variable so that 4000 we should place into the root then it start pointing to this node so simply M value we are storing into root variable we are storing into root variable or else suppose if it is not the first node if it is not the first node already nodes are there now important thing is 3 connections important first one is C this is one connection and here two connections required because four thousand you need to store here and so that it is connected to this one and next one we are storing like this it is because it is a double link it listener two-way connection is very very important so thousand we should store here and next four thousand we should store here next that four thousand you should store here also four thousand so then how can we establish the connection so three link sir which link first already we know right side links first left-side link next sir why simple suppose if you store 4000 directly into root variable then it stopped pointing then it stopped pointing suppose like this so suddenly right here if you place a 4000 directly here then root is stopped pointing to this one start pointing to this one then you loss this complete data that is the problem so that is why directly we cannot connect a left-side connection so root is still pointing to this first node only next right side connections you have to establish first right side connections you have to establish first right side connections means thousand you should store here so thousand means who is holding thousand root root value we are storing in two we are storing into this one this is the right location who is pointing to this node temp temp - right equals to M to write M - right next that is thousand will be stored here thousand this is one way connection is one way connection next here in this location we need to store four thousand four thousand means what is a temple yes temp value M value so will be stored in to which location which location here in this location who is pointing two thousand is a root is point in two thousand route to this location is a left or right left so route to left that will be stored in to route to left so that here it is in this place we are storing four thousand then it is pointing to this one so two connections are over and next last connection a root is start pointing to this newly created node it should stop pointing to this one simply a four thousand you should place here four thousand means what is the temp temp value we are placing in to route temp value we are placing in to route that is so here it is four thousand will be stored then it start pointing to this one so the node has connected in the beginning of the list this is how to create the node at the beginning okay so we'll see how to find the length of the list right and how to find how to display right all the elements of the list okay see so now so we will see how to find the length of the double linkedlist and how to display all the elements in the double linkedlist okay so first I will draw the list here first of all root is pointing to the first node we know that this is the first node suppose thousand thousand is pointing and this is a null data and next node link we are storing here some of the nodes we are writing to in suppose two thousand two thousand here is connection and next one previous node here thousand next one one more node suppose thirty three thousand here and next previous node address two thousand here and this is a null so total three nodes only we are taking three nodes so parallel you will see so because almost logic to find the length and to print the data is the same so here void void function is a length function or if you want to write clearly written type you can write a into because how many nodes are there in the list it will return this is to find the length and next parallel you will see the logic of a display also display all the elements in the list and here it is the length so first we are declaring one temporary variable struct node star temper and the initial value directly you can write a root also and here to find the length so we are declaring one variable count variable also count variable value zero initially this count starts with a zero only and here also in display also struck node star temp value starts with the root suppose here first we need to check how many nodes are there in the list okay but of course so no need to check if list is empty also no need to print the message because we are returning the length you is a zero okay so directly we are checking we are writing while temp not equals to null so what is the temple you temp value initially contains a root value now suppose here it is temp value initially root value thousand so it is not equal to null so here it is if root value is not equal to null it is a thousand nothing but already one node is there so if M not equals to null just increase the count value count plus place the count value become one so counting of first node is over now we need to check second node is there are not nothing but right side nodes are present or not we need to check here simply temp 2 into the temp we are storing temp 2 right value if right node is there or not we need to check if it is there that we are collecting into temp so what is the temp to write value is a two thousand so thousand become mm mm so then two thousand not equals to null because it is a while loop now as long as the condition is a true it will repeat so second time mm not equals to null the count value become two and again temp two right value is a three thousand because 2002 right is a three thousand again that is not equals to null so condition to come inside count value is a three and next attempt to write value three thousand two right value is a null three thousand two right value is a null so then here it is it will be failed because now temp value is a null null value not equals to null value condition false it will fail so directly it will return the count value written count value so suppose if no no elements in the list okay no problem suppose if no elements initially count value is a Girona that zero only it will return simple okay that is length and suppose just come to the display also same story right a temp value starts with a root only that is thousand but instead of counting just print the node value finish same logic while M not equals to null temp not equals to null here it is we are counting knife node is there instead of counting if node is present or not just print that one printf printf percentage D just like a linkedlist format arrow mark we are writing right we are writing M two data M two data we are printing why here it is a temp is pointing to initially thousand thousand two data is a 10 so that is what we call displaying the information after displaying this one next we should send the control to right side nothing but to the next node here it is M two right value will be stored into temp so like that it will execute but sir suppose if nodes are not there I want to print a message very clearly if nodes are not there in the list so then we need to display the message so sir what message we need to display right that is a list is empty message I want to display sir then you can write this logic inner if/else block okay suppose in a if block instead of writing this simply simply first we are checking if temp equals to null nothing but no nodes in the list directly printf list is empty list is empty simple or else if list is not empty then it will print so nothing but if nodes are not there right sorry instead of printing the blank messes I want to print some messes so then you can go for if-else or simply you can write a while loop also so like this while while temp not equals to null temp not equals to null just print the information of that node print that information nothing but a temp to data or information any variable you declared and next one we should send the control to the next node nothing but two right side we should move temp to right value this is the simple logic so two logics almost same one will count and one will print okay finally it is returning how many nodes are there in the list and here it is it will display all the elements in the list okay so this is all about how to add an element in the beginning of the list how to find the length of the list how to display all the elements of the double linked list okay hope you enjoyed this video for more videos please log in to nourish IT channel thank you
Info
Channel: Naresh i Technologies
Views: 107,540
Rating: 4.943212 out of 5
Keywords: Srinivas, Balu C, Data Structures, Learn Data Structures, Data Structures Demo, Online Data Structures Training, Hands on Data Structures Training, Data Structures Overview, Naresh IT
Id: mCTSzm1YcfM
Channel Id: undefined
Length: 18min 50sec (1130 seconds)
Published: Thu Oct 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.