Singly Linked List - Insertion Operation | Data Structure



In this post I will show you how we perform Insertion operation in Singly 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:











Copy Code
//Complete Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};

void display(struct node *t)
{
    while(t!=NULL)
    {
        printf("->%d",t->data);
        t=t->next;
    }  
}

//Function for adding node at beginning of list 
struct node *addbegning(struct node *start,int x)
{
    struct node *newnode;
    newnode=(struct node *)malloc(sizeof(struct node));
    newnode->next=NULL;
    newnode->data=x;
    if(start==NULL)
    {
        start=newnode;
    }
    else
    {
        newnode->next=start;
        start=newnode;
    }
    return(start);
}

//Function for adding node at last of list
struct node *addlast(struct node *start,int x)
{
    struct node *newnode,*temp;
    newnode=(struct node *)malloc(sizeof(struct node));
    newnode->next=NULL;
    newnode->data=x;
    if(start==NULL)
    {
        start=newnode;
    }
    else
    {
      temp=start;
      while(temp->next!=NULL)
      {
        temp=temp->next;  
      }
      temp->next=newnode;
    }
    return(start);
}

//Function for adding node at specific position  of list
struct node *addposition(struct node *start,int x,int pos)
{
   struct node *newnode,*temp;
   newnode=(struct node *)malloc(sizeof(struct node));
   
   newnode->next=NULL;
   newnode->data=x;
   
   if(pos==1||start==NULL)
   {
       newnode->next=start;
       start=newnode;
   }
   else
   {
       int i=1;
       temp=start;
       while(i<pos-1 && temp->next!=NULL)
       {
           i++;
           temp=temp->next;
       }
       newnode->next=temp->next;
       temp->next=newnode;
       
   }
   return(start);
   
}


void main()
{
    struct node *newnode,*oldnode,*start=NULL,*temp;
    int ch;
    
    do
    {
        newnode=(struct node *)malloc(sizeof(struct node));
        printf("Enter Data:");
        scanf("%d",&newnode->data);
        
        if(start==NULL)
        {
            start=newnode;
        }
        else
        {
            oldnode->next=newnode;
            
        }
        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(addbegning(start,y)); 
           break;
        }
        case 3:
        {
            int y;
            printf("Enter Data:");
            scanf("%d",&y);
            printf("Add node at Last of List:");
            display(addlast(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(addposition(start,y,p)); 
          break; 
        }
        default :
        printf("Invalid Choice");
        
    }
       
    getch();
}

Out Put :

Display Link List



Insert Node at Fist


Insert Node at Last



Insert Node at Specific Position



No comments:

Post a Comment