Insertion In Linked List

Single Linked List

At Begening 

void insert(struct node *start , int key)
{
        struct node *temp,*ptr; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data=key;
                ptr->next=temp;
                start=ptr;
         }
}

At End

void insert(struct node *start , int key)
{
        struct node *temp,*ptr; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                while(temp!=NULL)
                          temp=temp->next;
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data=key;
                ptr->next=NULL;
                temp->next=ptr;
         }
}

At Specified Position

void insert(struct node *start , int key , int loc)
{
        struct node *temp,*ptr;
        int i; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                for(i=1;i<loc;i++)
                          temp=temp->next;
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data=key;
                ptr->next=temp->next;
                temp->next=ptr;
         }
}

Doubly Linked List

At Begening 

void insert(struct node *start , int key)
{
        struct node *temp,*ptr; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data = key;
                ptr->next = temp;
                ptr->prev = NULL;
                temp->prev = ptr;
                start=ptr;
         }
}

At End

void insert(struct node *start , int key)
{
        struct node *temp,*ptr; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                while(temp!=NULL)
                          temp = temp->next;
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data = key;
                ptr->next = NULL;
                ptr->prev = temp;
                temp->next = ptr;
         }
}

At Specified Position

void insert(struct node *start , int key , int loc)
{
        struct node *temp,*ptr,*temp2;
        int i; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                for(i=1;i<loc;i++)
                          temp=temp->next;
                temp2=temp->next;
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data = key;
                ptr->next = temp2;
                ptr->prev = temp;
                temp->next = ptr;
                temp2->prev = ptr;
         }
}


Circular Linked List

At Begening 

void insert(struct node *start , int key)
{
        struct node *temp,*ptr; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data=key;
                ptr->next=temp;
                while(temp->next!=start)
                {
                       temp=temp->next;
                }
                temp->next = ptr;
                start = ptr;
         }
}

At End

void insert(struct node *start , int key)
{
        struct node *temp,*ptr; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                while(temp->next != start)
                          temp=temp->next;
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data=key;
                ptr->next=start;
                temp->next=ptr;
         }
}

At Specified Position

void insert(struct node *start , int key , int loc)
{
        struct node *temp,*ptr;
        int i; 
        temp = start;
        if(temp==NULL)
                printf("Linked List is Empty");
        else
        {
                for(i=1;i<loc;i++)
                          temp=temp->next;
                ptr= (struct node*)malloc(sizeof(node));  
                ptr->data=key;
                ptr->next=temp->next;
                temp->next=ptr;
         }
}

Comments

Popular posts from this blog

CS50 IDE (Best Platform for coding Online/Offline)

Top Websites That Will Teach You Coding For Free

Best Apps to do Programming on Android Platform.