Program for Implementation of Single Linked List Part-1 | Mr.Srinivas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome to nourish technologies this is Finn was so in this session so we are going to implement a single linkedlist so using C language syntax in all the previous sessions we discussed clearly theoretically right how to implement a linked list single linked list and how to perform all the operations like adding a node at the end adding at the beginning adding after a specified node how to delete a particular node how to display how to find the length all these operations we discussed theoretically now I just want to implement the program and I want to show how a single linkedlist executes exactly so header files we are including stdio dot H and link early single linked list means what is the dynamic memory allocation so so mostly we are using a malloc function because we know that a node is nothing but a structure type how we are allocating the memory dynamically to structure type variables so with the help of ml up function right so that is why ml log function is available in a std l ib dot h header file so we are writing that one as TD li b dot h so next main function main function we are writing and inside so what all the operations we can perform what all the operations we can perform on the single linkedlist that we can write in here it is more clearly if you want to specify so what program we are implementing here simply single linkedlist operations single linked list operations inside the main method also the pass first statement we are writing that is one printf single linkedlist operations single linkedlist operations /n but here it is if you write like this it will execute only one time already we have seen this approach in a stack implementation also continuously we need to perform operations so how we have to write all these things means so simply here all these things we need to place inside the while loop inside the while loop the condition is always true here inside we are writing inside so what all the operations we can perform first one is a how to add a node at the end of the list append is the first option and second option printf how to add the node in the beginning to add at begin add at begin next one printf add at after add at after next one printf function how to find the length of the list simply how many nodes are present inside the list that we are writing so four options so next one printf printf how to display all the elements in the list and next one how to delete element in the list nothing but simply how to delete a node in the list delete and finally this how to quit from the program so what is the seventh option how to quit from the program so all these operations we are performing right using while loop so why the reason continuously while loop executes until you select the option seven quit so whenever we are selecting this option seven quit so we will write the logic that executing exit function so then it will send the control out of the program okay so here it is first we are asking enter your choice enter your choice so they will enter that we have to collect the scanf scanf here it is we are reading percentage D the choice we are reading address of choice or simply CH here it is a CH is an integer variable that we are declaring inside the main method CH is a local variable no problem because this variable we are using always inside the main function only if you are using in all the functions of the program then we have to declare globally but here it is not required and here it is depends on the choice they selected so we need to write the switch case depends on the choice all these seven cases we have to write case 1 case 1 how to add a node at the end simply append we know that after every switch case we need to write the break statement and next this is case 2 this is ad at begin added begin and of course break statement and next it is case 3 so what is the case 3 add at after after a specified node how to add the node next case for length function length function and next break and next so what is the 5th option so what is the 5th option how to display the elements of nodes break and last one sixth option is how to delete the node delete break and the last option is case 7 just quit from the program we know that what is the function we have to use right it is a predefined function right that is a exit function so we are using suppose if they enter other than 1 to 7 then we know that which case execute default case printf simply we are giving invalid input or invalid choice anything we can get so depends on the function we are calling depends on the function we are calling all these things execute simple okay so now so if we are writing length function right break function display for all the functions we need to write the logic so first we are writing the logic logic for append function append function so we are performing a linkedlist operations but how to create the node sir first we need to define the structure structure should be a global why sir it is global means we are using that structure definition right inside all the functions nothing but a node creation functions like append function added begin function added after function everywhere we are using so that is why right above the main we are declaring that node type struct node struct node structure should ends with a semicolon and here it is a data is a integer integer answer what is the pointer type name is a link okay sir what is the pointer type one node is holding the address of another node nothing but one node is pointing to another node so node type is a main pointer type is a node type only struct node star and the variable is a link every node in a single linked list having two fields we know that so what are the fields one is a data type field that is integer next one is a Link field so that is a struct node type it is pointing to the next node and here it is globally we are declaring one variable struct node it is a permanent variable so what is a variable root variable sir what is the initial value of root variable though we are not initialized all the global variables are initialized with a default value or matically in a sea language sir what is the default value default values and null only I do not want to create any confusion so that is why directly I'm assigning the value null so though we are not assigning default value will be null so don't worry about that but anyway we are assigning the value so no D is ready and the root variable is also ready so here it is apparently we will write that structure also structure C for example so first root variable created root variable at a particular location root created and here it is initial value is a null initial value null it contains it is ready null value now now we need to create the node so how to create the node so that is inside the append function whenever they call append function so first we need to declare one temporary variable it is a local variable because after append function execution completion so then automatically the local variables will be deleted struct node star local variable name is a temp so temp also gets memory allocation temp get memory allocation at some location this is stem memory allocation how to allocate the memory so using a malloc function Emma log and here it is we are creating node so sizeof node we have to pass so it is better to depends on size of function sizeof struct node then what Emma log function will do means first it will create a node nothing but a memory will be created two fields two fields so fields having addresses also in the first node creation I will write clearly this is a data node consider and this is a linked node linked node and here it is a references also addresses suppose it is a 2 0 4 6 right integer occupies a 2 bytes consider so it is a 2 0 4 8 linked at it is created node so with the help of Emma log that I want to store that I want to store inside the temp variable inside the temp variable but here it is right Emma log function is written type is a generic pointer nothing but wide pointer type so here it is whenever we are trying to collect into temper temp is a struct node star type so this type conversion struct node star so this is creation so after this creation now we need to check how to read the information right how to read the information so from the particular I mean how to read the information from the end user simple here it is printf we are asking enter node data enter node data information we are reading so it will be displayed on the console so here it is a scanner function we are using scanf percentage d no data is integer sir into which location here you should specify the location who is pointing actually temp is pointing so first temp contains a - 0 4 6 - 0 4 6 so it is having a temporary connection with this temporary connection with this and next if you want to read the information into this location how to connect a temp to data address address means location address temp to data address you have to pass so here it is we need to write a temp to data m to data address this is a location to read the value so after reading into the next location suppose node value they had given 10 just consider and in the next location I want to place a null value so because no other pointer is there as of now so null pointer is there so how to place the null values are here null value into temp - link into em - link look Asian because temp only pointing to the node in the node link field is there in that field store the null value so this is reading information reading after reading this information so now we have to check right where we have to connect a node is ready node is ready where we have to connect this node either to route variable or somewhere else okay if list is already having some of the elements we need to add this node at the end of all the available nodes but as of now no nodes in the list so root should pointing to this location how it is possible root value is already null so temp value we are assigning to root simple so here it is first we are checking if if root equals to null if root equals to null nothing but node is not having any list simply here it is list is empty list is empty and then here we are writing simply the temp value we are assigning to root temp value we are assigning to root so here it is observed this value null value root values are null so now that value will be replaced with the new value is a temp value temp value is a 2 0 4 6 2 0 4 6 so then it start pointing to the newly created node that is of course first node in the list first node in the list so node is created node created ok so this is the way of inserting the first node into the linkedlist sir what about the remaining nodes what about remaining nodes right further we need to write the logic of else block using else block using else block remaining nodes we have to insert how to insert the remaining nodes observed how to insert so first for this logic right I am writing some of the nodes some of the nodes observe so this is a node this is a node such type of nodes we are adding we are adding is the second node already four nodes are there four nodes so one is used to store the data and the next one is the user to store the address so four nodes are ready and of course root variable is also ready root variable root variable it is holding the first node address just consider first node address is a thousand the thousand will be stored into root node thousand will be stored into root node so here here we are writing like this is pointing and next remaining nodes address is considered it is a two thousand two thousand next it is three thousand and next one it is a four thousand four thousand nodes always contains the data suppose data is a ten consider and link is a next road link two thousand so we do not know all these things I will explain all these things and here it is next node address is a twenty and node address is it three thousand and next one data is a 30 and address is a four thousand and next one address is of 40 and no other link so we are creating null value right so here it is a connections are there so first node is pointing to the second node right second node is pointing to the third node third node is pointing to the fourth node so as of now linkedlist is having three nodes mean four nodes four nodes now we have to create the new node so that we have to add at the end of the list and of the wrists just consider after four nodes insertion they have called append method once again I think but they just want to add one more right so first execution same right first attempt variable will be created temp gets memory allocation and inside the temper so value not it's stored mail log function will be created right next mail log function will call it will create a node it will create a node at a particular location at a particular location right node will be created suppose location is a 5,000 consider location is a 5,000 next here it is we are asking enter node data here it is that we are collecting into temp to data and of course first we are collecting the node address into the temporary variable so here it is a temp contains node address noted this is what 5,000 so temporarily it is pointing it is pointing temporarily to this node is a temporary connection and next here it is we are asking enter node data address of m2 data so temp is pointing to this node right address of this node means what this one here we are storing 50 and into the next location anyway we are storing the null value because it is last node I mean one more node is not it created this last node we are creating adding at the end simply right after storing a null value into location temp to link now we are checking observed now if block will not execute if block will not execute here it is a root equals to null here it is now check is the root value is a null value no it is not a null value it is not a null value right here it is so we are checking root value is not null it is a thousand so if condition has failed okay so else block execute so in the else block sir how can we add directly directly we cannot add we should take one more variable in the else block we should take one more variable struct node star just like P a local variable we are taking P gets memory allocation inside the P so what value we are storing sir we should travel from first location to last location who is pointing first location root so initially the root value we need to store inside the temple here it is here it is into P first we are storing the root value root value here it is a initially root value is a thousand right next we are checking if next node is there are not every time we have two checks or how many times we have to check we do not know so that is why we need to use a loop so here it is we are using the loop while while every time we need to check P - link not equals to null or not P - link not equals to null means what yes another node is there another node is there this is a simple logic P - link not equals to null or not we are checking here here so P to link p value is pointing to the first node first node P - link is not null it is a mm that means it is connected to another node so we should travel so p value we should send from thousand to two thousand thousand to two thousand so very simple logic so P - link value is a two thousand so that will be stored into P that will be stored into P finish so then here it is so thousand - link value two thousand so that will be stored here that will be stored and next one right how to move to the next location it will execute continuously as long as the condition is true so what is the condition P dot link not equals to null right if it is a null then it will stop or else it will continue 2002 link so value is a three thousand not null next three thousand - link three thousand - link is a four thousand it is not null next 4000 - link four thousand - link is a null value null value but here it is so whenever it is checking 4000 - is a null null not equals to null condition false then why loop will terminate so finally P is pointing to this node with the help of 4,000 is pointing to this node now very simple how to add just we need to add temp variable at the end of this one nothing but to the link so temp value is a 5,000 5,000 will be stored in to this location so 5,000 is nothing but @m after while loop execution once while loop terminated the temp value will be stored in - P - link because P is pointing to 4,000 P is pointing to 4,000 so 5,000 will be stored into the link location of a P so P - link so then the null value null value here replaced with the value that is 5,000 that is 5,000 so then the fourth node is connected to the fifth node connected this is simply how to add a node at the end of the list so this logic else block right if block executes only for the first node for all the remaining nodes which block executes savory time else block executes every time this is how to insert n node how to insert an omen that matter how to append a node to the single linkedlist okay so in the next session so we will continue this program what all the remaining operations right so that that will continue in the next session okay please watch all the related videos sort of this implementation thank you thank you for watching [Music]
Info
Channel: Naresh i Technologies
Views: 350,599
Rating: undefined out of 5
Keywords: Data Structures, 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, Srinivas
Id: roei8Vd5814
Channel Id: undefined
Length: 24min 14sec (1454 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.