Circular Linked List - Insertion Operation | Data Structure


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


Complete Program for Insert Node in Circular Link List:

Copy Code

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

struct node

{

    int data;

    struct node *next;

};

void display(struct node *t)

{

    struct node *str;

    str=t;

    printf("Start:%d\n",t);

    do

    {

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

        t=t->next;

    } while(t!=str); 

}

struct node *Cir_addbegning(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;

        newnode->next=start;

    }

    else

    {

        temp=start;

        while(temp->next!=start)

        {

            temp=temp->next;

        }

        newnode->next=start;

        temp->next=newnode;

        start=newnode;

    }

    return(start);

}


struct node *Cir_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;

        newnode->next=start;

    }

    else

    {

      temp=start;

      while(temp->next!=start)

      {

        temp=temp->next;  

      }

      newnode->next=start; //or temp->next

      temp->next=newnode;

    }

    return(start);

}


struct node *Cir_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!=start)

       {

           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;

            newnode->next=start;

        }

        else

        {

            newnode->next=start;

            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("Circular Link List is:\n"); 

           display(start);

           break;          

        }

        case 2:

        {

           int y;

           printf("Enter Data:");

           scanf("%d",&y);

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

           display(Cir_addbegning(start,y)); 

           break;

        }

        case 3:

        {

            int y;

            printf("Enter Data:");

            scanf("%d",&y);

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

            display(Cir_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 Circular List:\n",p);

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

          break; 

        }

        default :

        printf("Invalid Choice");

    }       

    getch();


OUTPUT:

CASE 1: DISPLAY



CASE 2: ADD AT BEGINNING OF CIRCULAR LIST




CASE 3: ADD AT LAST OF CIRCULAR LIST





CASE 3: ADD AT SPECIFIC POSITION OF CIRCULAR LIST






No comments:

Post a Comment