Doubly Linked List - Insertion Operation | Data Structure

 


In this post I will show you how we perform Insertion operation in Doubly Linked List in different ways. Like:

1. How we insert node at First ?

2. How we insert node at Last ?

3. How we insert node at specific position ?

I will give you a complete logic for every case.

Graphical Representation


Create Doubly Link  List





Insert Node at Beginning




Insert Node at Last




Insert Node at Specific Position






Complete Code

Copy Code

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>


struct node

{

    int data;

    struct node *next,*prev;

};


void display(struct node *t)

{

    while(t!=NULL)

    {

        printf("|%d|",t->prev);

        printf("->%d",t->data);

        printf("|%d|",t->next);

        t=t->next;

    }  

}


struct node *DBaddbegning(struct node *start,int x)

{

    struct node *newnode;

    newnode=(struct node *)malloc(sizeof(struct node));

    newnode->prev=NULL;

    newnode->next=NULL;

    newnode->data=x;

    if(start==NULL)

    {

        start=newnode;

    }

    else

    {

        newnode->next=start;

        start->prev=newnode;

        start=newnode;

    }

    return(start);

}


struct node *DBaddlast(struct node *start,int x)

{

    struct node *newnode,*temp;

    newnode=(struct node *)malloc(sizeof(struct node));

    newnode->prev=NULL;

    newnode->next=NULL;

    newnode->data=x;

    if(start==NULL)

    {

        start=newnode;

    }

    else

    {

      temp=start;

      while(temp->next!=NULL)

      {

        temp=temp->next;  

      }

      temp->next=newnode;

      newnode->prev=temp;

    }

    return(start);

}


struct node *DBaddposition(struct node *start,int x,int pos)

{

   struct node *newnode,*temp;

   newnode=(struct node *)malloc(sizeof(struct node));

   newnode->prev=NULL;

   newnode->next=NULL;

   newnode->data=x;

   

   if(pos==1||start==NULL)

   {

       newnode->next=start;

       start->prev=newnode;

       start=newnode;

   }

   else

   {

       int i=1;

       temp=start;

       while(i<pos-1 && temp->next!=NULL)

       {

           i++;

           temp=temp->next;

       }

       temp->next->prev=newnode;

       newnode->prev=temp;

       newnode->next=temp->next;

       temp->next=newnode;      

   }

   return(start);

  }


void main()

{

    //CREATE LINK LIST   -- START

    struct node *newnode,*oldnode,*start=NULL,*temp;

    int ch;

    

    do

    {

        newnode=(struct node *)malloc(sizeof(struct node));

        newnode->next=NULL;

        newnode->prev=NULL;

        printf("Enter Data:");

        scanf("%d",&newnode->data);

        

        if(start==NULL)

        {

            start=newnode;

        }

        else

        {

            oldnode->next=newnode;

            newnode->prev=oldnode;

        }

        oldnode=newnode;

        

        

        printf("Enter Choice: 1-Continue  2-Stop:");

        scanf("%d",&ch);

        

    }while(ch!=2);

       

    printf("What you want to do ?\n");

    printf("1-Display 2-Insert at First  3-Insert at Last  4-Specific Position :");

    scanf("%d",&ch);

    switch(ch)

    {

        case 1:

        {

           printf("Link List is:"); 

           display(start);

           break;

           

        }

        case 2:

        {

           int y;

           printf("Enter Data:");

           scanf("%d",&y);

           printf("Add node at Beginning of List:");

           display(DBaddbegning(start,y)); 

           break;

        }

        case 3:

        {

            int y;

            printf("Enter Data:");

            scanf("%d",&y);

            printf("Add node at Last of List:");

            display(DBaddlast(start,y));

            break;

        }

        case 4:

        {

          int y,p;

          printf("Enter Data:");

          scanf("%d",&y);

          printf("Enter Position:");

          scanf("%d",&p);

          printf("Add node at position %d of List:",p);

          display(DBaddposition(start,y,p));  

        }

        default :

        printf("Invalid Choice");

   

    }

      getch();

}


No comments:

Post a Comment