Data Structure

What is Linked list?

linked list is a linear collection of data blocks. In which linear (unlike array) doesn’t represent the actual linear order of their physical existence in computer memory. But instead, it represents that each element of data blocks points to the next element.

To more precisely, Imagine we have a discrete amount of data blocks in computer memory but each block knows exactly where the next block exists.This actually illustrates the concept of a linked list.

In general, a linked list is a sequence of data elements where each element knows it’s next element’s address in the computer memory. So, if we want to access an element we can to do that via its previous or forward element(in the double linked list).

Implementation of a linked list using C++ :

Before we implement linked list some points must be clear.

1.You must have the knowledge of Dynamic memory Allocation.

2.You have to know the basic feature of the Class. Also how to define user-defined datatypes with class.

3.And How to declare Pointer variables and how to access data with it.

4.Lastly, You have to know basic syntax of C++.

Our basic Idea to implement linked list is to, we take some memory with the help of  Dynamic memory Allocation and initialize those memories with our user-defined node’s values. Finally, we link those nodes with their internal embedded pointer variables in such manner that each node contains its next node address.

In below picture, I try to give you an overview how a linked list’s nodes linked to each other.

visualize linked list

Now we see the actual code (C++) to implement a linked  list and I also commented out for better understanding the code what I wrote:

#include<iostream>

class Node // we create this class to define user-defined data types
          // for node
{
    public:
        int num;
        Node *node; // this pointer variable points to next node by storing 
                    // next node's address in it.
                    // here node is a self referential variable
};

Node * head = NULL; // we initial head with NULL
Node * temp = NULL; //Later we need this to hold previous node address

void create_list(int data)
{
   Node *ptr;
   ptr = new Node(); // * here we create a node in heap with the help
             //of dynamic memory allocation with new keyword
   ptr->num = data;//* we initialize node's num variable with function argument(our data) 
                   // that is passed when function is called
   ptr->node = NULL; // * we initialize node with null it helps us to identify
                     // last node of the linked list when we accessing each elements
                     // with the help of Loop

   if(head == NULL) // * in this section we actually test it's beginning of the 
   {              // linked  list or not to initialize head with first node address
       head=ptr;
       temp=ptr;
   }
   else{
      temp->node=ptr; //* to initialize previous node with following node address
      temp=ptr; // we simply change temp value with following node's address
   }

}

In the above section, we actually declare a Linked List.Now we are going to pass data in it and also access the data In the main Function.

int main()
{
    int data,num;
    std::cout<<"please input the number of elements of linked list"<<std::endl; 
    std::cin<<num;
    for(int i=0;i<num;i++)
    {
        std::cout<<"please input the value of linked list : "; 
        std::cin<<data;
        create_list(data);// * we pass each data to create 
                          // linked list in {create_list(data)} function
    }

    // in this section we actually visualize the elements of the linked list
    
 for(Node *nod = head;; nod = nod->node)// * this statement  {nod = nod->node } 
   {                              // helps us to points to next element of the list
        std::cout<<num<<std::endl; 
       if( nod->node == NULL)//* here we check if the node consists of null pinter
        {                  // when its true we know that it's end of the linked list
            break;
        }
    }

    return 0;
}

Thanks for reading.I hope you find this article helpful. For any Information feel free to comment.Thanks.

 

1 COMMENT

Leave a Reply to What is Data Structure ? Why its needed ? How its come into Place? Cancel reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.