Skip to main content

Linked List Data Structure | Linked List tutorial

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

Popular posts from this blog

Disjiont Set Union-Find Data Structure | Code In C++

 Introduction:  In this tutorial we are going to write full program of disjoint set union find advance data structure in c++.  Problem Description: Disjoint Set Union (DSU) is an advance data structure, which basically uses in graph algorithms to find cycles. Codes:  Method1: Brute Force #include<bits/stdc++.h> using namespace std; int find(int f,vector<int>&dsuf){     if(dsuf[f]==-1)         return f;     return find(dsuf[f],dsuf); } void union_op(int from, int to, vector<int>& dsuf){     dsuf[from]=to; } bool isCycle(vector<pair<int,int> >&edge_list, vector<int>&dsuf){     for(int i=0;i<edge_list.size();i++){         int parent1=find(edge_list[i].first,dsuf);         int parent2=find(edge_list[i].second,dsuf);         if(parent1==pare...

Linked List Data Structure | Add Two Number Explanation

Introduction: In this tutorial, we are going to solve a very famous problem on Linked List. It is also an important question for coding interview. so let's see the problem and understand it in detail. Problem Statement: There are two numbers and each digit of number is represented by a node of linked list.and we have to add these two numbers, which is given in the form of linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 eg: num1 = 2354;       num2 = 875; see the image below: Solution: Now let's solve it. In this problem there may be three possibilities. First: Number of Nodes in the first linked list is greater than second linked list. Second: Number of Nodes in first linked list is less than second list. Third: Number of Nodes in the both linked list is equal. Here number of nodes means number of digits in both number. Another thing we have to keep in mind is that if we add two digits then there may be possibility of gen...

Integrating Frontend to Backend | MERN stack(100% working)

How to Integrate frontend to backend in MERN stack? For integrating the frontend to backend just do two things. Step1: Go and install cors on both side (front end as well as backend)              To install cors simply open the folder into terminal and type npm install cors--save. step 2: just go to your front end section(react app) open package.json file.                 and just add one line into package.json file.           "proxy":"http://localhost:4000" here i am using 4000, so what is 4000 ?. this is the port  on which our backend is listening. so it may be possible that u may have other port number on which your server is listening. so put yours port number on which your backend server is listening. and this proxy thing we have to add in frontend part not in backend part. Now it may be possible that you want to know ...