Transcript for:
Lecture on Linked Lists

Friends, in this course of data structure, we had seen the linked list in the previous video. We had seen what is needed: linked list, why we need linked list, what will be the benefit of doing linked list, I had also shared the notes with you guys Now what am I going to do here, first of all I will tell you how linked list can be created. And after that we will see that how the same linked list can be traversed. If here I show you the notes that I made for you people: So it looks something like this Here I wrote everything about linked list to you. I hope everyone has access by now These notes of the linked list must have been accessed by all Here I told you that directly in the array the elements that are stored in the contiguous memory location The elements inside our linked list are stored in non-contiguous memory location. That is, first the data, and then the pointer to next node. then the data, the pointer to the next node. And this thing keeps going till it becomes NULL the last one which is my pointer So if I'm in the linked list here I can write here Linked list And here I will create the linked list first so i will write here Linked list and then creation First of all creation will come of linked list and then after that I will tell you the traversal also, ok Traversal So we are going to see the creation and traversal of linked list: Okay So here let's talk about linked list first. I will not show you people by making array again and again, I will only make linked list here. First of all we will have the data here, let's say 7 after that it will be a pointer this is my: int it's mine, a pointer, okay and what kind of pointer is this struct Node * is of type, right, and what is the type of this whole If we talk about the type of this whole, then what is it? that is the struct Node. struct Node This is the struct Node, this is the pointer to the struct Node What is the meaning of this that it will point at a struct Node and I create that struct Node from the back, the struct Node this is complete: So here you guys look, I'm going to make another struct Node. Suppose I made it something like this, and it's 8 here and it's another pointer that will point to the struct Node again So what does point mean, point does not mean that the pointer has a hand and has a finger and is pointing with a finger Pointing means that it is a variable and this is its address. If its address is let's say 788, then 788 is stored inside it, ok 788 is stored inside it And because its address is stored inside it, so we say that it is pointing and we show this thing by becoming arrow. There is nothing like that there is a link made in your RAM, inside your memory some such strings are connected, which will be pointed, no. It is storing only its address And when it stores the address, it is called point to store the address. When a pointer stores the address of another struct Node here, I would say it is pointing to this struct Node Okay And this is my first Node, this is my first Node, this is called the first node So i write here first Node And it can also be called head Node. first node or Head Node And here it is my second Node, so here we write the second Node or second Node and so on, Okay. Third node, fourth Node, fifth Node in this way I can create over here For the implementation that is required, I have already told you people as much. But what we will do here is to implement it and see what will happen in the end, when it becomes NULL. Then I'll declare the end of this list here, okay So here I am declaring declare the end of this list So here I am announcing the end of this list. declare the end of this list To declare the end of this list, that is, I have made the pointer NULL. I said look here my linked list is over Because the pointer that is this one is now pointing to NULL, ok so i hope you guys got this thing cleared Now over here if I am in the notes then you guys will see here that I have implemented it with the help of Struct node. Make sure that it is which (s) it should be small, you people will know the syntax wise it is here (s) or struct Node* next; struct Node in this way we can make so if i write something like this and then make your linked list So what should I do, first of all I have to make a head Node then i have to make second Node then after that i have to make the third Node, if i make a list of 3 Nodes Obviously, I'll have to link them So how do i link them first of all i will say something like this look first, i have to make head Node Then i will write head = (struct Node *) malloc(sizeof(struct Node)) And what will happen by doing this, I will show you here by writing I'll write something like this, I would say look give me the head that is there, And I want some memory in the heap so I am telling heap god please give me memory by typecasting it in struct Node * type equal to this Whose equal to? malloc(sizeof(struct Node)) So give me such a memory for which I will keep a node, okay, give me enough memory that I can store a node That is, it contains this int data, and it is a pointer So you will get a head, and you can use that head Make sure here that you understand that it is not a structure, it is a struct pointer What is this? a struct pointer And we talked here that to access the data of the struct pointer, you can use the arrow operator, okay And I emphasised the arrow operator too much And I was getting a lot of comments in such a way that I repeat things sometimes So I will not repeat here now, I will point you here, at the place where you people will find the arrow operator So here if I show you people, our C in one video, you have to watch this video C language tutorial for beginners in Hindi In this, I have given you notes, I have explained the whole thing to you in 15 hours. You can exactly navigate to that part and see if you have any issue So I'm here to tell you people but still, people who want revision If you want head data then you can simply use arrow operator like this You can say, the data of the head:, this is equal to 7 Look at you here, 7 data Here's 7 data, so here i will write, head data = 7 And after that, the way I have made this head, in the same way I will also create a second Node. second Node So what shall I write here, i have to link second Node and head Node I will say head and then I'll write here You guys look at the head here, I have made a next, have made a pointer named next So I'll write here, next = and what is it storing, what is this, it is a structure pointer and this is also my structure pointer So to link these two, I will simply write something like this second Node, Okay So do one thing, you have got this idea and so on And I'll make the third Node, the fourth Node, and finally, i will NULL the third Node that's next to me, okay. So let's go to the VS code, and in C language, to see the creation and traversal of linked list Let's talk a little bit about traversal, before that let's go to the VS code Here I have talked about of the linked list both the creation + traversal So let me tell you guys about traversal also. So if I talk about the traversal of linked list here So see how the traversal will be You need to go through the list one by one to do the traversal. Here as if this is your one node, after that it is linked as if with some other node And then you'll have another node over here, and the pointer to this one will be linked to that node. and so on until your null comes Now it is not necessary that it has only 3 elements it can have 10 elements, it can have 100 elements. it can also have 50 elements. What you should mean this is the pointer it is where it becomes NULL, there your list is over So here I have written 7, here is written 8 here is written 22, here is written 11 So what will I do over here, i'll make a pointer I'll point it here ptr, okay and then I'll point to this whole node that ptr and what i will do i will make this pointer point here again I will make its pointer point to its next, then I will make it point to its next, then make it point to its next and as soon as its next becomes NULL I'll say over here that's done now, stop printing now OK, so here's an idea of traversal So the traversal can be like this And if (n) elements are in your linked list, then here is the time complexity of traversal O(n) You guys also note this here, okay So here if you are going ahead, you are going to visit every node one at a time. So traversal's time complexity is O(n). because if (n) nodes are in the list then (n) times you are travelling the list And the complexity that gets O(n) according to this, Okay I hope this thing will be clear to you guys And what will I do now that I will go to the VS code And after going to the VS code, I will tell you the right way how you can creation and traversal of the linked list. And here we go, just in VS code So guys, here we have taken a very good idea how to do traversal in linked list, and how to make linked list Now here we have the time actually to make linked list So if I talk about the video number, then by opening the playlist, the video number is going to be 14. So video number 14 because this is going to happen I'm a 14_LinkedListTraversal.c I create a file with the names of it ok, and put a boiler plate in it What will I do here before inserting the boiler plate, I will define my structure here And how would I define the structure? i will write, struct Node and then I'll write here, int data; and after that i will write, struct or oopss here i will write, struct and then I'll write, Node * next; Okay that is, it is a pointer to a struct Node type and it will point to a structure like this well, don't forget to put a semicolon (;) here it's important in the structure Now here I have made a structure now what i will do is first make head Node so what shall i do here Here I am first of all a struct aaa oopss I accidentally pressed a button here man I don't know what I pressed on the keyboard struct Node * head = i will write, struct Node * head = and it'll get a little bigger here So here I write something like this, Or let me declare it first and then write here, head = And here I will first typecast from struct Node * The way we do int * malloc, we can also do struct Node * malloc or (sizeof) and over here struct Node Okay, (sizeof(struct Node)); So this is how (sizeof(struct Node)); has come here And here head I wrote Then second = , similarly I will write and head over here And then I'll write here, struct Node * second and after that I will write here, third, ok or head, second, third And after this here I will write something like this second or third, Okay So here's how I made it all, okay Created 3 Nodes and where will I find them, they will meet in the heap, ok Allocate memory for nodes in the linked list in Heap I'll find it in the heap You have to keep this thing in mind, it will not be found in the stack. You'll find it in the heap. I have done this dynamic memory allocation. Once I have done dynamic memory allocation like this Now i can link these Nodes so what should i write, i have to write What should be the data of the head, as if I am doing 7 And what should be the next to the head, that should be the second, Okay So i write here link first and second Nodes The way I linked the first and second Node in the same way I will link the second and third Node as well. So i write here, second and third Nodes Here's what will be my next of second The data of the second: I make it 11 And the next of the second will be the third and then here I'll write Terminate the list at the third node Okay So after the third node I want to terminate the linked list. so i will write here Do the data that is the third, let us say 66 And do the next to the third one, NULL Make sure that this NULL is written by yourself, it is written in capital And at the same time make sure that we are using malloc include too <stdlib.h> So it's a linked list program I hope this thing is cleared to you It has 7, 11 and 66 Let's run it and see, no error is being thrown through Because I haven't done anything print in it yet. so i will not expect anything from this program I will simply run this program And after running this program I will expect that no error will come. so over here i ran this I am hoping that there should be no error There is no error because everything is fine What will I do now, the way I told you about traversal here: I will talk about the traversal of this list And I'll show you how to traverse this list with the help of a pointer by moving this pointer one by one, how can we make a function of linked list traversal? What do I do to make a function of traversal of linked list Let's make a function here So here I'll make a function and it won't return anything here Obviously, it'll just be void So I make it only after the structure I will write here, void and i will write here linkedlistTraversal And what will it take, this is just what I want (struct Node * ptr) i need pointer I want a pointer that is of type struct Node * And it's pointing, where at the head node Okay Once I got this pointer which is pointing here in the head node. So what will happen that with the help of this pointer, I will print the elements inside the whole list one by one. How will it be possible? It should be possible that first my pointer will point to the head node then after that I will find the element next to the head node. Because here I have one data:, and at the same time I also have the next node: So I'll find out the data and the next node here and i get this 8 again And then I'll print the data and find that next node and run that data And I'll keep doing this until it becomes null, Okay Which is this node its next until it becomes NULL Till then I'll keep doing all this So to linked list traversal, I have given a pointer which is of type struct Node *: so i wrote, struct Node * ptr, I get one Now what will I do here, I will "printf" here I will say see what to do, do it %d and give it to me here What the data of it data of the ptr I could have written him a node instead of a ptr, but that's fine. ptr data, Okay You know that this pointer is pointing to the struct Node type Since it is a pointer of type struct Node, so I wrote struct Node *: here With the help of "printf" I printed its data. I will update this ptr now I would say, ptr = ptr ->next; What will happen to that Is this the pointer here, the pointer which was pointing to the earlier is, what will it do now, will see, what is here, And here's the thing, so it doesn't point here, but he's going to point here. I will do the same thing back, ptr = ptr ->next; So it's not pointing here, it's going to point directly here i.e. will point to this whole structure, and then I can print its data. and then not pointing here, will point here, and for how long do I want to do this? until it points to NULL So I'll wrap this whole thing in a while loop and i will write here while(ptr != NULL), Okay And by wrapping it in the while loop, I'll do something like this And I do the format document, so that it is well displayed. And you guys can see that I've done this till then, until the ptr becomes NULL And as soon as the ptr becomes NULL, it will stop printing my list I do one more thing over here that element I write something like this, (Element), and put colon (:) And after that I put a back slash (), So that you know, the element here is this Or The elements that I have will be printed directly, so I run this I didn't even call this function So i should have call the function first So i will call the function named linked list traversal linkedlistTraversal And I'll give it head What is the head? head is a pointer, of struct Node * type So over here this is, struct Node * ptr Okay So what will I do here, I save it and run it. And you guys see, Element 7, Element 11, Element 66, it's got me over here If it also had a fourth node if it also had a fourth node i.e. I would put some data in the third node, let's say So i write here link third and fourth nodes and i will write here third ->data, third ->next And to this, I will do the fourth So what will happen here, over here, 41 will come and third -> next, will be fourth And I haven't made the fourth, so I have to make the fourth. So look, made the head made the second, made the third, and made the fourth, all right. I made four nodes Now I have linked third and fourth like this Changed, 41 in third's data, and fourth in third's next Now I'll do the same with the fourth 66 in fourth's data, and NULL in the fourth's next So what will happen head's data is 7, head's next is second Data in second is 11, second's next is third next to third is fourth, next to fourth is NULL That is, the linked list is getting terminated on the fourth. So now I should get 7, 11, 41 and 66, ok So as soon as I print it out here, you guys see, I've got 7, 11, 41 and 66 over here If I talk about what was being printed earlier, then 7, 11, 66 was being printed, I will show you people by enlarging it a little. So 7, 11, 41, 66 are all getting printed instead of 7, 11, 66 alone Okay So here's how I make my list If I add more elements to my list, in my linked list So this is my linked list traversal function so it keeps going on till it ptr becomes NULL. It'll keep printing it, keep doing it, and keep doing it Okay So as soon as my last element is found here, that is, as soon as I get this element, It will see here that now the ptr next to it is NULL That is, now I cannot point at any structure. So while loop which is the condition which is its, it will become false its condition was, ptr != NULL But the ptr will now be equal to Null's That is, now it will traverse here for the last time. Because now the ptr which is next is equal to NULL I have spoken, ptr = ptr ->next, over here And now because the ptr is pointing to NULL Finally when it increases, then ptr, eventually will point here, towards NULL. and this will be the termination of the while loop here And this condition which will become false So the linked list traversal function will stop running here. This loop will stop running and linked list traversal function will return actually, ok Will not stop running, will return So when it returns, it will stop running. So, hope this thing is cleared for you. Made the head, second, third, fourth, allocate the memory for all of them, dynamically inside the heap Then set the data of the head, link the head to the next set the second data, linked the second to the next Linked second's next to third, third's next linked to fourth's, fourth's next linked to NULL And here I have declared that this is the end of the list. This is the last element of the list because the next one is pointing to NULL So here's the chain, it's closed Sometimes I had to add an element, can do it at any location, so I can by removing the NULL here, creating another node, put a fifth node So we will see the insertion etc. But for now I would just like to say that you guys here of four element, five element, 10 element here are able to make linked list of it So, it's good for you I hope this thing clears all the things, which are traversal of linked list, and creation of linked list If you haven't accessed this list yet So I would say that you guys should access this playlist because I have uploaded all the videos of the data structure. and will do so in future. Relatively, very few people have access to this playlist I would say by bookmark it and save it And if you guys want me to bring this kind of course for you guys free of cost So please take a screenshot of this playlist and must share it on Instagram I will put the link of my Instagram below And you guys don't forget to tag me If you tag me, then I share the story by tagging you back. So don't forget to tag me if you do, I'll know you're sharing this playlist, and I'll definitely tag you And I've tagged all the people Whoever shared the course in Data Structures, probably the one person shared on Instagram So far maximum 2-3 people must have done so if you do then thank you so much Out of so many people, only 1 or 2 people are coming forward to share So I really thank you very much, whoever are sharing this So don't forget to tag me Below I have given the link of my Instagram, in the pin comment Notes: You must have got all these by now I will make available to you people this code Don't worry you about this code, this code is yours, I will give it to you You bookmark the playlist, and share if possible Like if you like the video Thankyou so much guys for watching this video And i will see you next time.