LINKED LIST (CREATION AND DISPLAY) - DATA STRUCTURES

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello folks welcome back to our channel so in the previous session we have seen the advantages and the limitations of errors so in this session we'll go with the list implement linked list what's the difference between the array and linkedlist so in the previous session we have discussed that in a day the elements are stored in contiguous memory locations right so here in list that is not required that means the elements need not be stored in contiguous memory locations the elements can be stored in any location but as the name itself indicates there will be a link between one element and the other element so we have to establish a link between the one element and another element that's why these elements are need not required to occupy in the contiguous memory locations for this for this we need to create a structure for a node so here an element is represented as a node element is represented as North because here it nor consists of two fields so previously in the air is element means only one field that is a value so let it be either 10 or 20 or 30 right and so on so this is called elements but here Lord here Lord means it consists of two things first one is data field second one is industry address or pointer field or next field right in this data we have to insert the value that means this time so it may be integer flow characters string or anything else the actual value will be stored in data what about this field the additional field in this additional field the name itself indicates it will represents the address of next node address of next node so this is the link of another element that's why the elements need not require to occupy the contiguous memory locations so different elements can have a different memory locations but here we are just establishing a link so one element I mean in every element there will be and one field called address field which spends its own address of the next node so that's why we have to consider it has a node rather than element so how to represent this node representation how to represent eight node so a node is represented in a blue field see so this is the data fringe this is the next place this is the data field is the next fit then if if list contains two elements or three elements that use three nodes so if list contains three nodes see let us take ten and the number location is thousand twenty the memory location is 2013 location is three thousand right so three three nodes so 10 20 and 30 the address of ten is twenty two thousand there are no soft auntie's so how do we present these England's C and 2020 so here this is the we know that this is the data this is the address of next one so here we have to store the address off next no what is it it's no 20 what is the address of 22,000 so here the 2000 DB here over your second row 20 is the value and the corresponding next that means at this address field it will seal the edges of next smooth the what is an external 30 so what is the address 3000 so 3,000 will be stored here coming to the third board that T is the value and what is the next to next three that means either screen so here that is the last element so obviously it will be model so that means the last element then it was free maybe surely not so it indicates the list the end of the list it indicates the load is end of the list right and this legal list will be having the head dead thing the two other concepts that is head and tail so heavy the head will be the starting element they will be the enjoyment head take the name of the loader right so head and tail are name of the Lord the first load name is head thus last note name is take so if it is having some 40 tell me with the 4000 address again we have to use the another mode that is 40 and here we have to insert 4000 and the tag will be here hope you understood this one right so an oar consists of two fields so so far we have studying regarding these elements only the element but here we have to treat it as a node because it consists of two fields right so before going to the program first we have to create a structure for this mode so if it is an element we can directly write without datatype we have we can declare it with a data type that is int or a float or double or anything else right so by using the primitive data types we can declare the variable for assigning the element but coming to this node how to declare a node so here it is having only one element so we can directly use the primitive datatype here it is having a two fields so for every node there will be two fields right so how do declare the so we had a data structure which can accept multiple elements of different data types so here the data can be anything that was any any data type right so it can be integer float character or a string whatever it may be right so second one is purely endless so we know that if the in order to store letters we have to create a point we have to create a pointer so this is the amount more primitive data type this is a pointer variable so we have studied in a C language is say in order to add two different elements with different data types what we have to implement we have to implement this structure structure right so structure is a collection of multiple elements with different data types the same thing how to apply by declaring a node because it consists of retired letters first we will declare a node and then we will insert the elements or we will create the list and then we will display in this session hope you have understood the basic representation and how the node is represented and how the load I mean part of the things available in this nor now let us see the current border yeah here we'll be right data and next we can simply write cut nor we have to create two things one is data next so simply we can write an integer value in the data second one is a pointer which is pointing with the cell floor right so which is pointing the cell phone so we have to create struct node star next right so this is the Declaration of a No so here we have to declare a variable that is some star see this is fun data and next next is a pointer variable but it is a self pointer because this is also a field of the same rule it is also a field of same node so we have to write a pointer variable of the same node so that is called the start node so if you want to declare a variable C if you want to declare a variable so just right so let it let it be let it be consider this structure so into data flow percentage let us take this one so if you want to create this variable variable what we have to do so we have to the syntax of structure variable is start nor some s 1 right start to load s 1 is used to declare a structure variable C so yes one will be consists of data and the percentage isn't it right now here the same node must consists of a pointer so how to create a point available struct node P that is a pointer variable pointer structure variable so the same way we are using the same thing start node star next that means self pointer the point down right so this is called self referential structure the name itself will get self-referential structure so reference means the pointer self that means which represents the same node which is a part of the same node right and here instead of using the normal structure variable we have to use or we have to declare the pointer structure variable because if it is a longer structure variable C hope you understood this one right self-referential because what is header on these next which is also representing the symbol so for this we have created this one so normally if you create the structure variables but lord new structure with you it will be having a DITA next it excuse me next so here this is a problem because here there is value there's next is others so we can't store the address in normal variable so we can store the address in pointer variable so for this purpose we have to create the pointer variable so all the structure variables for these nodes are pointer variables star you hope you understood don't get confused so if it is normal structure variable that variable will be having two fields data in next data is normal data type and next is a editors so a normal variable can store the address so in order to show the address we have to create the pointer so that's why we are creating the pointer variable now this pointer variable will be having the data and address so this is possible so next after creating a structure we have to allocate the memory so we have seen the drawback of memory allocation is the main drawback for arrays there is the static memory allocation so here for every node we can have a dynamic memory allocation by using malloc function right next we have to allocate the memory dynamically new typecast for which we have to apply I mean I can remember is that normal star pointer variable this is Mel walk how much memory should be allocated size of the sizeof operator is used to little this size or chip I'd buy that particular data size of Lord so this is for dynamic memory allocation so this statement is used to allocate the memory independent so for every node for every node before it's it's getting the value we have to allocate the memory dynamically so this is first and I cast what type of form what type of data type we are allocating the memory function is used to allocate the memory so how much memory should be allocated then is the size of so the size will be calculated that will be allocated to the new here new is it very new is just a variable point the structure pointer variable so you will be having both data next hope you understood this one right so this is all about the interaction now we will create the real-estate disciplines so as you have said that every list will be having the head and the head will be the starting position and the day will be the last position so initially initially we have to assign the head and tail to the first element because if there is a one element we have to assign the head and head to the same language same node if you keep on creating a list you if you keep on adding the list we have to change the thing okay I hope you understood so if there is only one load that itself is a hidden thing if it is having your two loads the first one will be the head and the second one will be the tail Tennessee so this is a structure so I will write the structure here right creating a list creating a list so if you want to create some 10 20 and 30 see first we have to allocate the memory and then we will get the 10 and initially it will be null mn no no this will be the hang this will be the tape okay hey take you next second iteration the new will be this one in the second iteration the new will be this one that means whatever the image we're inserting that will be them you'll now see here head is 10 right 10 is 10 no the address position is none now if it is not an empty list it's not an empty list right it is a list with volunteering so head will be fits it head will be fixed okay now coming to the thing what we have to do we have to establish the link first establish the link first so what we have to do first tale of next so he attained takeout also nodes right so similarly star new similarly star head star tail so heading tail are also the nodes tail off next that means the tail of next this one will be same with you hope you understood this one tale of snakes here tavis this one tell Nana tale of Nexen that means the address of address part of a team will be assigned with new so let us take the address positions thousand two thousand three thousand now what is the new address of new 2000 so here it will replace the mouth with mm right next they should be change now the tail will be new the tails within you so that's why again you will have the second element that is 20 and now it is null okay hope you understood initially so initially the new it's a new the new will be some garbage and lung so whenever you are taking 10 this garbage value is replaced with 10 and the address bar will be a man address position will be null so headed thing will be assigned to new initially initiated right see and whenever you are inserting the 20 element first we have to establish the link so this is very important first one means established early next create no okay created so a is equal to new this one so here the tag is replaced and it is a saint here okay so head is having the two fields 10 mm and the tail is having the fields 40 and love no once again in this next iteration the new will be third we will be turning now again the first we helped establish the live end of next tale of next so this is the next this is the data right so tale of next is equal to new newest 30 right 30 of that the memory allocation that the memory address of 30 is 3000 so that will be a saint to tale of next so this is very important we have to follow this this thing this is the most important first establish the link so 3,000 and then take is 14 you say 17 right so thing will be same here right so this is for creating a list so we have to apply this in loop so that how many number of elements will be created in the priest right so I will write the program and i will explain the logic I mean we will trace that first let us write the code and we will trace it so after creating this one we have to use a memory location dynamic memory location some mu is equal to write node star M anak size of world so no every time the new will be allocated C so we keep this in loop so for every time then you will be happy so new start more star block self so once the irrigation is done then right then we have to take the value stand up some person HD leave the value and assign the value to head you off so here it is in pointer right this is a pointer structure variable so we know that the pointer structure variable accessing the data members is done by using the added so if it is a normal structure variable that variable you can access the members for using the dot operator if it is a point structure variable it should access the members were using the adding I mean the arrow bot data is equal to value and new of next will be initially initially right now we check the condition for head if it is having the single element or multiple events so if head so head is equal to is equal to null so we have created the variables but we are not and its si so if head is equal to is equal to null what we have to do both head and tail should be two head is equal to new because both with points to the first element itself n is equal to yes condition that means if it is having multiple elements right so then we have to follow the same procedure that means well first we have to establish the link and then we have to I said change the tail pay off next is equal to MU this is for establishing the link L is equal to new right so this ends the cool now just right bring them why to continue and exit flash stadium because we have to lay the character so really the characters can have percentages see Amazon see H close the window well we now see it is equal to continue the loop until see it is equal to well wherever you press Y n automatically it will be exited right hope you understood this one this is for creating creating the list creating the list so if you place this one if you trace this one let it be some party with a thousand thirty with the three thousand from 15 with the five percent the memory locations okay now see first value so just above this loop you just declare the value see hit I have skipped everything right just I am writing the logic first egg value so value is equal to some 20 value is equal to 20 now new of data so new consists of data is equal to value that means here 40 here it will be no next Hecht is equal to is equal to null so initially we have declared this head and tail but we are not initializing so automatically head will be consisting of some garbage and null similarly take will be also having some car Belgian no takes also having the garbage value and now now see if head is equal to is equal to null yes additional it is null then head is equal to new so head will be again 20 and null similarly tail is also pointing the same heading tank will be 20 n no now if part is standing I mean if part is executed so else will be ignored automatically why to continue engine if you press Y again it will check the condition see H is equal to Y so it is true again it will go with the new now the new will be the this ends with the first iteration this ends with the first iteration so now there is a node 20 and null and the new the new will be the next rule that means the memory will be allocated for the new and scan of value some value 30 will be assign so that they will be are saying to new of data place a 30 year and next is knowledge here it will be you know then if head is equal to is equal to another so this is the head 10 thing right this is the head end thing okay now head is not another because head is pointing the first element obviously then this condition will be failed as what will be executed in their spot in of next tale of next that wins null here out of it will be done so that will be a same with the new so new address is represent so that will be assigned to tail of next so here here the mouth will be replaced with click on them and then is equal to mu nu tail is equal to u so obviously one board will be created so here 30 here well and the tail will be replacing with this one right this ends obviously again why to continue any changes if you continue with why it will go with here it will check the condition its again - again a new that means iteration completes here again the new is a memory location is created and where you scan value except 15 so 15 and another right if head is equal to null head is not enough because it is pointing with some element so this part will be node else part will be telling a of next here K is 30 but C 23 throws in 30 so this is the head this is the tail so take off next head of next is this field so this is the next field is equal to you whatever what is the address of new so the address of News 5000 so well we'll be decreasing with five of them and then a is equal to new so obviously a tail will be created here with the 15 and the thing will be removed here and it will be a saint yeah right next printf why to continue integer if you have press again that while checking the condition CH is equal to en that will set false automatically comes out from the loop so finally the list will be 20 30 and 50 so 20 the load 20 is pointing the address of 30 and the note 30 pointing the address of 50 and the node 50 is having null because the node 50 is the end of the list right so hope you understood this one the creation of a list a simple thing a creation of a list and then the second one is we have to display the list display the elements of a list see so we will see the display elements and now we will count here we will see the display and we'll stop in the next sessions we will go to the next our operations see here we have created already some notes 23 throws in finding the next node that is 35,000 for display so we have to move the head to the MD so till the head moves to the empty we have to display the kind every room so further we will take a one more variable called M temp is also similar to a dente so first we'll assign the head to temp so obviously the temp will be here the temp will be here next I don't know why temp not equal to null temp not equal to null right what we have to do we have to print everything printf percentage D temp of data so initially it will be head so temp of date and 20 will be printed and next we have to move the head to the next one so we can't do the head to the next one so that's why we have assigning the head value with a temporary variable M so M is equal to M of next close this one simple so this is the Muny logic for displaying the list so let us trace here so first temp is equal to head here temp will be here this one next at M 1 equal to none so through temp of data we print in here next temp is equal to temp of next so now temp will be temp of next that means it is pointing with the this one temp of next that means this 3000 address of 3000 means this one now again M not equal to another it will check with the null condition so it is again a 2 so temp of data that means a 30 will be printed and again temp of next so it will be a single this one now again temp not equal to null yes so temp of data so 50 will be printed and the temp is equal to temp of legs there is no other load so that's why it will be assigned to no so tan not equal to null but here the temp is null see here here the temp is mal the condition fails automatically it will stop so we are having the three nodes with 20 30 50 and here also here displaying the 20 30 50 right so here we have to create a temporary variable and just we are moving the temporary variable to end of the list so whenever the temp is equal to null automatically we can stop the loop we can terminate the look I hope you understood this one so don't get reduce it is a very easy concept so a normal element means the storing a single value but here the list consists of node which is having two fields one is a data field and another one is the address field so here a variable consists of a multiple elements with the different data types is represented as a structure so here we have to create a structure and in that structure a normal variable can't store the address so we have to create the debtor's that means a pointer variable which represents the same that's why we are using the self referential structure and also the variable should have both the value and the address so we are creating the variable as a pointer variables for representing a normal right so here the main advantage is the elements can be stored anywhere because we are establishing a link between one element and another element right and also we are using the dynamic memory allocation for the allocating the memory so hope you understood so we have covered the the list introduction and how to create a list and how to display the list so we'll stop here and in the next session we will go with the insertion of an element in different positions at the beginning and the middle position and the ending position so how to insert an element at the beginning middle and end so in the air s we have seen if you want to insert an element at the beginning all the elements should be moved towards its right side and then we have to insert the element so that doesn't happen in the concept of list so that we will see in the next session so insertion and deletion to mode we have to see right so let us stop here so if you really understood my sessions like my sessions shave my sessions with your friends and if you are having any doubts please feel free to post your dogs in the comment section so that I will definitely try to clarify on your terms and no forget to subscribe to our Channel thanks for listening thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 231,964
Rating: 4.9071646 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, dynamic memory, structures, list creation, list display, lists operations, interview, placements, cse
Id: 3hyxc4juJRg
Channel Id: undefined
Length: 42min 18sec (2538 seconds)
Published: Fri May 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.