• Recent Uploads

    How to create linked list in c with Program and Algorithm[types of linked list and full explanation ]

    what is link list?

    In computer science, a linked list is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.


    types of link list:-



    • Singly Linked List
    • Circular Linked List
    • Doubly Linked List

     Singly Linked List (SLL)?
    The simplest kind of linked list is a singly liked list which points to one link using pointer. It has two parts, one part contains data and other contains address of next node. The structure of a sLL is down below. here we have used structure to create a link list in c.

    Circular Linked List?
    A circular linked list is a linked list in which the last node points to the head or front node making the data structure to look like a circle.  A circularly linked list node can be implemented using singly linked or doubly linked list.
    doubly linked List?
    In singly linked list, we can move/traverse only in one single direction because each node has the address of the next node only. Suppose we are in the middle of the linked list and we want the address of previous node then we don’t have any option other than repeating the traversing from the beginning node.
    To overcome this drawback, a doubly linked list can be used. In this, there are two pointers. One of these pointers points to the next node and the other points to the previous node.

    Advantages:-

    Dynamic Data Structure
    Linked list is a dynamic data structure so it can grow and shrink at runtime by allocating and deallocating memory. So there is no need to give initial size of linked list.
    Insertion and Deletion
    Insertion and deletion of nodes are really easier. Unlike array here we don’t have to shift elements after insertion or deletion of an element. In linked list we just have to update the address present in next pointer of a node.
    No Memory Wastage
    As size of linked list can increase or decrease at run time so there is no memory wastage. In case of array there is lot of memory wastage, like if we declare an array of size 10 and store only 6 elements in it then space of 4 elements are wasted. There is no such problem in linked list as memory is allocated only when required.
    Implementation
    Data structures such as stack and queues can be easily implemented using linked list.

    Disadvantages of Linked List

    Memory Usage
    More memory is required to store elements in linked list as compared to array. Because in linked list each node contains a pointer and it requires extra memory for itself.
    Traversal
    Elements or nodes traversal is difficult in linked list. We can not randomly access any element as we do in array by index. For example if we want to access a node at position n then we have to traverse all the nodes before it. So, time required to access a node is large.
    Reverse Traversing
    In linked list reverse traversing is really difficult. In case of doubly linked list its easier but extra memory is required for back pointer hence wastage of memory.
    If you know some other advantages and disadvantages of linked list then please mention by commenting below.


    Disadvantages:



    • They use more memory than arrays because of the storage used by their pointers.
    • Nodes in a linked list must be read in order from the beginning as linked lists are inherently sequential access.
    • Nodes are stored incontiguously, greatly increasing the time periods required to access individual elements within the list, especially with a CPU cache.
    • Difficulties arise in linked lists when it comes to reverse traversing. For instance, singly linked lists are cumbersome to navigate backwards[] and while doubly linked lists are somewhat easier to read, memory is consumed in allocating space for a back-pointer.
    ALGORITHM FOR CREATING A LINKED LIST:


     PROGRAM/SOURCE CODE



    1. #include<stdlib.h>
      #include<stdio.h>
      int i ,n,x;
      struct node {
       int data;
       struct node* next;
      };
      
      struct node* head=NULL;
      void nodecreate(int x)
      {
       struct node* temp=(struct node*)malloc(sizeof(struct node));
       temp->data=x;
       temp->next=NULL;//here we are making the isolated node
       if(temp->next==NULL)
       {
      
       head=temp;
      }
      else
      {
       while(temp->next!=NULL)
       temp=temp->next;
      }
       /*this will be work for when the list is empty or having elements in it
       let see when list is empty means temp->next =NULL;
       then it will assigned as the
       head=address of(temp);
       else
       if there are some elements in list.
       */
      }
      void display()
      {
       struct node* temp =head;
       while(temp!=NULL)
       {
       printf("\t%d-->",temp->data);
       temp=temp->next;
       }
      }
      int main()
      {
      printf("how many nodes sir");
       scanf("%d",&n);
       printf("enter the values in linklist");
      
       for(i=0;i<n;i++)
       {
         scanf("%d",&x);
       nodecreate(x);
       display();
      }
      
      return 0;
      }
      


                                                     OUTPUT


    PLZ... Share This Site To Your Contacts.....




    With regards

    Team EveTech