Doubly linked list 

Doubly linked list

  • A Doubly linked list can be known as another type of linked list.
  • It is named as doubly linked list because it consists of two addresses, while a singly linked list consists of a single address.
  • The doubly linked list consists of three nodes, one for the data part and other two for the previous and next node.
  • The previous pointer holds the address of previous node, whereas the next pointer holds the address of next node.
  • We can traverse from both the side backward as well as forward.

There are various operations that can be carried out in doubly linked list :

  • Creating doubly linked list
  • Insertion in beginning
  • Insertion in random
  • Insertion in last
  • Deletion in beginning
  • Deletion in random
  • Deletion in last

Now lets see the various programs for doubly linked list :

1 . Creating linked list


    # include <stdio.h>
    # include < stdlib.h>
    

        struct node
        {
            int data;

            struct node *prev;
            struct node * next;
        }

     void create ( )
    {
        struct node*temp,*p;
        int n,i;


        printf("enter the size");
        scanf("%d",&n);


        for( ; n > 0 ; n-- )
        {
            temp=(struct node*)malloc(sizeof(struct node));
            printf("enter the value");
            scanf("%d",&i);


            temp→data=i;

            temp→next=NULL;

            temp→prev=NULL:

            if(root==NULL)
            {
                root=temp;
            }
            else
            {
                p→next=temp;

                temp→prev=p;

            }
                p=temp;
        }
    }

2 . Insertion in beginning

    # include <stdio.h>
    # include < stdlib.h>

    

        struct node
        {
            int data;

            struct node *prev;
            struct node * next;
        }

    void addinfirst ( )
    {
        struct node*temp;
        int i;


        temp=(struct node*)malloc(sizeof(struct node));
        printf("enter the value");
        scanf("%d",&i);


        temp→data=i;

        temp→next=NULL;

        temp→prev=NULL;

        if(root==NULL)
        {
            root=temp;
        }
        else
        {
            temp→next=root;

            root→prev=temp;

            root=temp;
        }
    }

3 . Insertion in last

    # include <stdio.h>
    # include < stdlib.h>

       struct node

        {
            int data;

            struct node *prev;
            struct node * next;
        }

    void addinlast ( )
    {
        struct node*temp;
        int i;


        temp=(struct node*)malloc(sizeof(struct node));
        printf("enter the value ");
        scanf("%d",&i);


        temp→data=i;

        temp→next=NULL;

        temp→pev=NULL;

        if(root==NULL)
        {
            root=temp;
        }
        else
        {
            struct node*p;
            p=root;


            while(p→next!=NULL)

            {
                p=p→next;

            }
            p→next=temp;

            temp→prev=p;

        }
    }

4 . Insertion in random

    # include <stdio.h>

    # include < stdlib.h>
    
        struct node
        {
            int data;

            struct node *prev;
            struct node * next;
        }

    void addinmiddle ( )
    {
        struct node*temp,*p;
        int i=0,loc,n;
        temp=(struct node*)malloc(sizeof(int));


        if(root==NULL)
        {
            printf("linklist is empty\n\n");
        }


        else
        {
            printf("enter the location");
            scanf("%d",&loc);


            printf("enter the value");
            scanf("%d",&n);


            temp→data=n;

            temp→next=NULL;

            tmp→prev=NULL;

            p=root;
            while(i<loc)
            {
                p=p→next;

                 i++;
            }    
            temp→next=p→next;

            p→next→prev=temp;

            p→next=temp;

            temp→prev=p;

            printf("node is inserted\n\n");
        }
    }

5 . Deletion in first

    # include <stdio.h>
    # include < stdlib.h>

         struct node

        {
            int data;

            struct node *prev;
            struct node * next;
        }

    void deleteinfirst ( )

    {
        struct node*temp;


        if(root==NULL)
        {
            printf("linklist is empty");
        }


        else
        {
            temp=root;


            root=temp→next;

            temp→next=NULL;

            temp→prev=NULL;

            free(temp);
            printf("node is deleted");
        }
    }

6. Deletion in last

    # include <stdio.h>
    # include < stdlib.h>

    

        struct node
        {
            int data;

            struct node *prev;
            struct node * next;
        }

    void deleteinlast ( )
    {
        struct node*temp;
        if(root==NULL)
        {
            printf("linklist is empty");
        }
        else
        {
            struct node*p;
            temp=root;
            while(temp→next!=NULL)

            {
                p=temp;
                temp=temp→next;

            }
            p→next=NULL;

            temp→prev=NULL;

            free(temp);
            printf("node is deleted");
        }
    }

7. Deletion in random

    # include <stdio.h>
    # include < stdlib.h>

        struct node

        {
            int data;

            struct node *prev;
            struct node * next;
        }

    void deleteinmiddle ( )
    {
        struct node*temp,*p;
        int i=0,loc,n;


        if(root==NULL)
        {
            printf("linklist is empty");
        }


        else
        {    
            printf("enter the location");
            scanf("%d",&loc);
            temp=root;


            while(i<loc)
        {
            p=temp;
            temp=temp→next;

            i++;
        }
            p→next=temp→next;

            p→next→prev=p;

            free(temp);
            printf("node is deleted\n\n");
        }
    }

8. To display the data in linklist

    # include <stdio.h>
    # include < stdlib.h>

    
        struct node
        {
            int data;

            struct node *prev;
            struct node * next;
        }

    void display ( )
    {
        struct node*temp;
        temp=root;


        if(temp==NULL)
        {
            printf("linklist is empty\n");
        }


        else
       {
            while(temp!=NULL)
            {
                printf("%d\n",temp→data);

                temp=temp→next;

            }
        }
    }


Linked list Part 1

What is Data structure ?

Classificaton of data structure