Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Insertion in Linklist at Different Places

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 501
    Comment on it

    Insertion in Linklist:- The Insertion of Element in Singly linklist at different places like: insertion at beginning, last and middle in the linklist. In below code I will show you how to insert the element in linklist at beginning, middle and last.

    insert_beg():- This function insert an Element at beginning in the linklist and move the pointer start to the newly inserted node.

    insert_end():- This function insert an Element at Last in the linklist and move the end pointer to the newly inserted nodes.

    insert_mid():- This function insert an Element at middle in the linklist for that have taken two pointers temp and p initially both will point at the start pointer and after first iteration the p pointer will point to the next element and temp pointer will point one element behind where the p pointer will points.

    See the code example below-

    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    #include<process.h>
    
    struct node
    {
    int info;
    struct node *next;
    };
    typedef struct node NODE;
    
    NODE *node=NULL,*start=NULL,*end=NULL;
    
    void create();
    void insert_beg();
    void insert_end();
    void insert_mid();
    void disp();
    
    void main()
    {
    int ch;
    clrscr();
        while(1)
        {
    
            printf("\n1:Create Linklist");
            printf("\n2:Insert Element At Begining");
            printf("\n3:Insert Element At Middle");
            printf("\n4:Insert Element At Last");
            printf("\n5:Display");
            printf("\n6:Exit");
            printf("\n\tEnter Your Choice:-");
            scanf("%d",&ch);
    
            switch(ch)
            {
                case 1:
                create();
                break;
    
                case 2:
                insert_beg();
                break;
    
                case 3:
                insert_mid();
                break;
    
                case 4:
                insert_end();
                break;
    
                case 5:
                disp();
                break;
    
                case 6:
                exit(1);
    
                default:
                printf("\n\tOption Not Available");
                break;
    
            }
        }
    getch();
    }
    
    void create()
    {
        node=(NODE *)malloc(sizeof(NODE));
        printf("\n\tEnter the Element:-");
        scanf("%d",&node->info);
        node->next=NULL;
    
        if(start==NULL)
        {
          start=node;
          end=node;
        }
        else
        {
          end->next=node;
          end=node;
        }
    }
    
    void disp()
    {
    NODE *temp;
    temp=start;
    
    
        if(start==NULL)
        {
            printf("\n\tNo Element to Display Please Insert Element First");
        }
        else
        {
            while(temp!=NULL)
            {
                printf("%d\n",temp->info);
                temp=temp->next;
            }
        }
    
    }
    
    void insert_beg()
    {
        node=(NODE *)malloc(sizeof(NODE));
        printf("\n\tEnter the Element:-");
        scanf("%d",&node->info);
    
        node->next=start;
        start=node;
    
    }
    
    void insert_end()
    {
        node=(NODE *)malloc(sizeof(NODE));
        printf("\n\tEnter the Element:-");
        scanf("%d",&node->info);
    
        node->next=NULL;
        end->next=node;
        end=node;
    
    }
    
    void insert_mid()
    {
    int flag=0,no=0;
    NODE *temp,*p;
    temp=p=start;
    
    
    
        node=(NODE *)malloc(sizeof(NODE));
        printf("\n\tEnter the Element You Want to Insert:-");
        scanf("%d",&node->info);
    
        printf("\n\tInsert After Which Element in Linklist:=");
        scanf("%d",&no);
    
        while(p!=NULL)
        {
           if(p->info==no)
           {
            flag=1;
               node->next=p->next;
               p->next=node;
               break;
           }
           else
           {
            temp=p;
            p=p->next;
           }
        }
        if(flag==0)
        printf("\n\tElement You Want to Insert After is Not Present in Linklist");
    
    
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: