 Hello friends, I am Sanjay Gupta. I welcome you to Sanjay Gupta Tech School. In this video I am going to compare array with link list. So we will discuss which is better and in which case you can use array or the index case you can use link list. So basically I am going to compare these things. First I will be explaining the use of variable. Then I will tell you why we use static array. Then I will tell you advantage of dynamic array and then comes link list. So this is basically a path to which you can learn the requirement of link list. So whenever you begin learning programming or you want to write some programs, so you start with a variable. So what is variable? Variable is a battery location where we can store some values. So let's say if you want to store one value, so do a linear variable and you can linear one value into this variable. So if you want to perform operation on a particular value then you can declare a variable. Now your requirement changes. You want to store more than one value. So what you can do? You can simply declare more variables like if you want to store three values. So you can declare three variables A, B, C or whatever their name will be. So what happens? Three separate memory locations went to there and at same point of time you can store values into these three variables. So this is about variables. Now this is the advantage of variable. Now let's discuss about the product so that we will move to end. Let's say you want to store 50 elements. If you want to store 50 elements, so how many variables are required? 50. So isn't it easy to declare 50 variables? No, because we need to figure out the names, unique names on those variables. Then let's say if you are reading those values using print-up scanner, you need to use format specify again and again and then you need to write the variable names. So let's say if you are able to declare 50 variables then in case of 500 elements, it is impossible to declare 500 variables and manage that program because 500 variables are very large. So this is the problem now, so this problem is and we can divide adding two types like static array and dynamic array. So first time we have to tell you about static array and then we will discuss about dynamic array and then finally we will reach to limitless. So now we need to store 50 elements or 500 elements or 5000 elements. So if you want to store these quantities of elements, then instead of declaring static variables, you can create an add. Now if you want to declare an add, so this will be the same. And then in square bracket you can mention the size of that. If you want to store 500 elements, you can declare array length. So what is the value of using add? We have one variable that is capable to store 50 elements all together. Again, name of variable is a and this k variable is capable to store 500 values all together. So now I hope you understood the requirement of array because variables, one variable can store one value. So if you want to store more than one value, you require that amount of variable. But using of array is reducing number of variables to one variable we can manage and number of elements. So this is the advantage of adding and let go static array. Now you need to understand the difference between static and add. So let's say we declare this array and we mentioned size as 50. And after writing the complete program, when you execute your program, you need to process these 50 elements. So now my question is, while executing the program can we change the size of this array? Can we increase its size or decrease its size? That is not possible. That's why it is static array. So in case of static memory allocation, at the time of program execution, you cannot change the size of that array. So this is the disadvantage of using static array. So here comes the use of dynamic array. So static array, in case of static array, we need to provide size at the time of writing the program or you can say at the time of compilation. At the time of execution, we cannot change its size. But if you want to decide memory allocation at one time or execution time, then you can create your array dynamic array. So for that, you need to use pointer and malloc function. So now I'm going to tell you how you can create dynamic array. So first to create dynamic array, we need to declare pointer. So let's say at the time of compilation, only p pointer will be available there. Now, to read size of the memory, we need one more variable, that is n. So now we need to read like how many memory allocation are required or how much value users have to store. So for that, you need to read n from user. So you can write scanner for 70 and percent n. So the value of n will be provided by the user at one time. So at one time n will be having a particular time. Now you can use a function that is malloc. So you can write this statement in as far as that malloc size of n multiplied by n. So this statement is responsible for allocating memory at runtime. So here you cannot say what will be the value of n. It will be provided by the user while the program is executed. So let's say n is 5. So 5 blocks will be allocated and the base address will be provided to p. If value of n is 10, so 10 blocks will be allocated and the base address will be provided to p. So this is your dynamic array. So in this case, what will happen? User will decide how many elements a user wants to store. So accordingly on runtime memory will be allocated and the value will get stored. So I hope you understood all these three things, variables, static array and static array. So in case of variable, we have limits like we cannot declare large amount of variables. We need to decide their names. We need to manage them. So we have a solution to that problem that is static array. But static array is also having a problem that we cannot decide size of memory at runtime. So we solve that with the help of dynamic array. So user will provide size and we will be allocated the value. So now you might be thinking like we have dynamic array and what is the requirement of limiters. So now I need to tell you the topic of dynamic array so that you will convert or you will get to know how we can convert that topic into advance case with the help of limiters. So I am taking this example. So let's say you are executing your program and enter value of n as 5 and 5 will be allocated and after this you need to store 500. But after memory allocation and processing, your requirement changes. You need 10 memory locations to store the values. So what will happen? You will use another function that is realloc. So realloc does it reallocate the memory that is allocated by manor. So what will happen? The existing values will be removed and you block of memory will be allocated. Now again after realloc, your requirement again changes. You want to increase or decrease size again. Then again you will realloc. You will use realloc method. So what will happen? Again, memory size will be decided and accordingly you need to store the values. And the previous values will be changed and you need to store all the values again and again. So this is the problem here. So now solution to this problem is linked list. So in case of linked list, what happens? If you want to store one value, so one memory block will be allocated. Then if you want to store another value, then another memory block will be allocated. So if you are storing a value, then only memory will be allocated. So you don't need to decide how many values you will be storing. So let's say you have a value, you will allocate that value and then store that value. Then you have another value. You will again allocate that value and then store the value. So this name linked list works. So we don't need to read size of memory altogether. So the problem of dynamic edit is the advantage of using linked list. So whenever you are facing a problem like you are again and again allocating memory while executing the program. So in that case you can prefer linked list. So now I'm going to tell you how the linked list works and how it occupies memory locations. So it is very easy to understand. So similar to dynamic edit. If you talk about linked list, so again we need to find that. So first I am explaining the linked list structure in a particular way. Then I will tell you like how you can create a structure for that linked list with the help of C language. So let's say this is a pointer and I am naming it as star. And initially it is having one. Now you executed your program and you want to insert a number. So you will implement your program in such a way like if you are inserting a number. So first you need to allocate a empty block and then you will be inserting a number. Then again if you want to insert a number so a new block will be allocated and existing block will not be modified. So in new block you will be storing a new value. So how it happens. So let's say you want to store a number so a new block will be allocated. It is known as node. Let's say it is node 1 and it is infinitely divided into two parts. First part is known as info or you can say it is value. So whatever name you want to get you can get to this position. And this location will be a pointer to next. So if you want to store a number so this memory will be allocated and you get store number here. Next you want to store that so your number will be stored here. And this pointer next will be added because we don't have any more nodes. Their values will be stored. So as you create your first memory location. So it's base headers will be stored in this point. So let's say base headers of this memory location is 0.01. So this value will be stored. So now you can imagine or you can relate why it is linked to this because memory locations are linked with the help of memory addresses. So if you are on this pointer so through this pointer you can access this address and you can move to this location and you can access this address. Now if you want to store one foot value so you will create one foot memory location that will be moved to. So you can imagine we are creating new location and we are not modifying this location. This value will remain as it is. But in case of dynamic adding if we use realloc so it reallocates whole memory structure. But that doesn't happen in case of digitalism. So move two will be divided into two parts. So this will be equal and this is pointer to next. Let's say it is going 20 and the second part will be having null and it's base address let's say 2001. So it will be stored here. So now if you are on start so you have address one, 001 so you can move to this location. If you are on this location so it's second part is another address that is the other one. So through this you can move to this. So this way you can create any number of nodes and you can store values as per your fit one. Now if you want to delete any particular node let's say we have one more node that is node T. It is having 30. It's base address is 3001 which is stored here and it is null. Now this is the insertion process. Let's say you want to delete a particular node so what will you do? Let's say you want to delete this node. So you can simply erase this and you can put null. So if this node next part is having null it means this node is automatically disconnected from this link. So this way you can easily delete a particular node from the link. So when you will be deleting or inserting nodes you don't need to shift all the elements like we do in address. So if we take an example of an array, all of these values are available and if you want to insert an element between two and three. So you need to shift all the elements that you can insert an element. But in case of link list let's say we have this memory. Now if I want to insert any element in between. So what I can do? I will create one more node that is node number 4. Let's say it's base address is 4001. It is having values 14. So what I can do? I will store this node's base address here and this node's base address here. So now if we traverse this link list, so if we are on start so we are having address as 1001 to be used here. Then this node's next part is having address as 4001. So we will directly move here. It is having 2001 so we will move here and then we can move here. So this way you can easily insert any element without moving any other value that is available in the link. And similarly if you want to delete a particular node. Let's say if I want to delete this node from the middle of this link list. So what I can do? I can store this address here and I will be deleting this memory location so that this will be removed from this link list. So 1001 is having 2001 in the next part so it will be linked with this and this will be linked with this and this will be removed from the link list. So insertion and deletion is very easy in case of link list because we don't need to shift each and every element like we do in address. So this way I hope now to understand the requirement of variable then why we move to static array then why we move to benefit array then why we move to link list. So this way I hope you understood the whole comparison between all available data structures where we can store values. So basically all the store values. So in some case memories are located at compilation type. We cannot change those memory allocations at runtime but in some cases memory is located at integration type. So you can allocate memory as per your requirement. So variable and static arrays are compilation type memory whereas dynamic array in link list occupies memory at 5 parameters and as per the requirement of the list. So this way I hope you understood the difference between array and link list. If you want to watch more data structures and algorithms related videos so you get full description of this video. You will find links of playlist so you can follow that to learn more about data structures and algorithms. Thank you for watching this video.