What is Linked List?
->Linked List is a data structure. it means it is a way to store our data like array. there are some benefits for which we use Linked List over array which we will see later.
->In practical way, Linked List is collection of Nodes.
->It is a linear data structure. don't think too much, just understand one thing linear means a straight line .
so what i want to say is it stores data and forms a structure like a straight line, or linear means we can say that we can traverse it using a single for loop. this is enough for basic understanding about linear data structure. another linear data structures are Array, Stack, Queue etc.
->let's understand it using some practical examples: you can take any examples in which there is a concept of linking of one to another, you can take an example of a crime thriller movie, where to catch the murderer, police finds the links of one with another and at the end they reach to the murderer.
what i want to say is one one node has the address of another node. and so on.
->As i said earlier it is a collection of Nodes(or boxes). so lets see how the Nodes should look like and what should be the structure of each node.
->In each node there will be two field one field to store the data and another field to store the address of next node(or simply information about the next node).
You can see in the above image. at the top there is a structure of node. and at the bottom there is a collection of node which is our linked list. so this is the visual representation of node and linked list.
let's understand it by writing a small piece of code.
so lets first discuss about Node and after that we will make the linked list by connecting them.
as we have discusses earlier each node will have two fields.first one is to store data(of any types like int, char etc) and another field to store the address of next node. so to store two filed in the same box we can't use int, char etc. so we have to define a structure. i hope you know about the structure.
struct Node{
int data;//it may be char or whatever type of data you want to store.
struct Node *next;//pointer to point next node.
}
don't get scared we will see each and every thing in detail.
be focused here:
In the first line i made a structure and called it "Node" okay.
you can see the structure of node in the above image.
Next line is int data; so this is to store our data, it may be int, or may be of other data type.
Next line is most important: struct Node *next;
so lets try to understand what it means.
first of all it is a pointer variable. name of this variable is "next" and data type of this variable is struct Node(this data type is defined by ourselves). and if you know little about pointer, pointer basically points to the address of the variable of same data type.
lets make it little bit clear:
if we have an int variable, then to store its address we should have a pointer of type int.
that means data type of the variable and data type of the pointer should be same.
here we are pointing to next node(in which there are two fields) that's why to store nodes address we should have a pointer of type struct Node. cause each node is pointing to next node. and to store the address of next node we should have a pointer of type struct Node
let's see the above image again. i have created a linked list. and each node have its address below the box, and in the second field we are storing or pointing to the address of next node. that's why pointer should be of type struct Node.
i hope you got my point, if you still didn't understand just comment it down.
Comments
Post a Comment