Creating a Single Linked List (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this presentation we will learn how to create a single linked list in c programming first of all I would like to tell you that in the previous presentation we have seen already that how to create a node of a single linked list now in this presentation we will learn how to create a complete single linked list so let's get started here I would like to tell you that this is the list we want to create basically list must consist of three nodes apart from this that the data that I have given over here as an example is 45 98 and 3 and the linked part of the first node should contain the address of the second node and the linked part of the second node should contain the address of the third node apart from this the third node link part should contain null there must be a head pointer which should point to the first node of the list so this is a single linked list which we want to create now my question is how to create a node of the list that is the first question we already know how to create a node of the list isn't that so we just need to create a structure something like this struct node it should consist of a data as well as the link pot data and the link part this is the way we will create a node of the list this is what we have learned in the previous lecture we know that how to create a node of the list now we want to create three such nodes and we want to link them together this is our task for this purpose of course we first have to create the first node of the list and that is what we have done over here in the main function you can see we called malloc function to allocate memory for this node obviously we need to allocate memory for the node it is not sufficient to create just a structure we need to allocate memory for that as well and that is why I have passed this parameter sizeof struct node and you can see over here that I haven't done any typecasting as I told you already this is not necessary although it is a good practice but it is not necessary now here is the head pointer which is pointing to the first node of the list this is clear from this fact that is after allocating the memory the address which is returned by this Milaap will be the address of this node that is one thousand this address will get stored within this head pointer now this head pointer can access data as well as the link part of this node and that is what is done over here the head pointer is accessing the data and Here I am trying to assign this value 45 to it here in the link part I'm trying to assign null initially it contains null later on it will get updated obviously if we want to create the second node we again have to call this Mel of function and then after that here what I am doing I am putting the address returned by the may log into this head pointer now head is pointing to the second node of the list and obviously with the help of head pointer I am trying to assign 98 to data and null to the link part but let me tell you there is some problem with this code you can easily identify the problem as well basically you can see that head pointer is pointing to the second node of the list isn't that so head is now pointing to the second node now there is no way to access the first node of the list we have lost the reference of the first node previously head was pointing to the first node now it is pointing to the second node now there is no way to access the first node of the list or there is no way to access this node at all right because the only way to access this node is through this pointer now it is pointing to the second node so what is the solution here we will create another pointer which will point to the second node here we will change this to current instead of head and then now this current pointer is pointing to the second node of the list head pointer is still pointing to the first node now this is fine because we haven't lost the reference house the first node at all head is still pointing to the first node and there is another pointer current which is pointing to the second node so everything is fine up to this point now the next step is to update the link part of the first node so that it can point to the second node so here this is the way we update the link part of the first node the link part of the first node now contains the address 2000 because here I am initializing this with current current contains the address 2000 that is why head link is equal to current gives this address that is 2000 means the link part now contains this address 2000 which means that now the first node is connected to the second node isn't that simple okay friends this is it for now this is part 1 of the lecture creating the single linked list in the next presentation we will create the third node of the list and we will try to add that node to the list we have created so far [Music] [Applause] [Music]
Info
Channel: Neso Academy
Views: 125,901
Rating: 4.9482536 out of 5
Keywords: single linked list, creating the node of a single linked list, single linked list creation, single linked list code, code of single linked list, single link list code, single linked list program, program of single linked list, node of single linked list, first node of single linked list, creating a single linked list, data structure single linked list, single linked list data structures
Id: nxtDe6Gq4t4
Channel Id: undefined
Length: 4min 36sec (276 seconds)
Published: Wed Jul 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.