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...

Target Sum | Backtracking Problem

Introduction: In this tutorial we are going to solve a problem "Target Sum" which is from leetcode. and believe me it's really a good problem to understand Backtracking(Recursion). and if you try to understand the problem as well as code you will get a clear picture of Backtracking. Problem Statement: Link To Problem You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and - . For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers equal to target S. Input: nums is [1, 1, 1, 1, 1], S is 3. Output: 5 Explanation: -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 There are 5 ways to assign symbols to make the sum of nums be target 3. Solution: As given in question, we have two operation + and -. so we will make recursive call for + and - . and if we have sum as target and we have reached upto the last ind...

Group Anagrams solution with explanation:Leetcode solution

what is Anagrams? suppose we have two strings and the characters present in both of the strings are same. then both will be called anagram of each other. eg:" rat" and "art" both are anagram of each other. the characters present in both string are same. eg: "raat" and "art" these are not anagrams of each other. and the reason is that the frequency of "a" in "raat" is 2. while the frequency of "a" in "art" is 1. so these are not anagrams of each other. in short two strings will be anagram of each other if they have same characters and the count(frequency) of each and every character must be same in both strings. Solution: There are different ways to solve this question, so here we are going to use Hashmap to solve this question. cause whenever there is need to search we should go with the data structure which use balanced binary tress or hashing. and we know that Hashmap  uses balanced binary trees. so using...