Insertion in linked list
Insertion at First node:
To insert at first node we have to make new node , that node points towards head node and it become head note .
Algorithm:
1.Declare a PTR node .
2.Point the PTR node towards head node.
3.Head node become second node.
4.Traverse all nodes.
5.EXIT.
Source code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void trv(struct node *ptr)
{
while (ptr != NULL)
{
printf(" Element is : %d\n", ptr->data);
ptr = ptr->next;
}
}
struct node *insertionatfirst(struct node *head, int data)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
ptr->next = head;
ptr->data = data;
return ptr;
}
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = NULL;
trv(head);
head = insertionatfirst(head, 0);
trv(head);
return 0;
}
2.To insert an index of node
To insert after an index of a node ,we have to declare or decide the node . Create a 'p' pointer which points toward head node and then run the loop until we reach the just before the index.
Algorithm is :
1. Declare a index , p pointer and ptr pointer
2.Run a loop of p pointer until we reach previous index of index.
3.Set ptr->next = p->next and p->next = ptr
4.Traverse all the nodes .
5.EXIT
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void trv(struct node *ptr)
{
while (ptr != NULL)
{
printf(" Element is : %d\n", ptr->data);
ptr = ptr->next;
}
}
struct node * insertionatindex(struct node *head, int data,int index)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
struct node * p = head;
int i=0;
while (i != index-1)
{
p = p->next;
i++;
}
ptr->data = data;
ptr->next = p->next;
p->next = ptr;
return head;
}
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = NULL;
trv(head);
printf("\n\n\n");
head = insertionatindex(head, 56,3);
trv(head);
return 0;
}
3.Insertion at end of a node:
Algorithm:
1.Create a node with given value
2.Define a node pointer and initialize with head
3.keep moving until it reaches to the last node in the list.(*p!=NULL)
4.Set p->next = ptr and ptr->next = NULL.
Traverse the list.
EXIT.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
};
void traversal(struct node * ptr){
while (ptr != NULL)
{
printf("The element is : %d \n",ptr->data);
ptr = ptr->next;
}
}
struct node * insertatend(struct node * head, int data){
struct node*ptr=(struct node *)malloc(sizeof(struct node *));
ptr->data=data;
struct node * p = head;
// int i = 0;
while (p->next!=NULL)
{
p = p->next;
}
p->next = ptr;
ptr->next = NULL;
return head;
}
int main()
{
struct node * head;
struct node * second;
struct node * third;
struct node * fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next =second;
second ->data = 2;
second ->next = third;
third ->data=3;
third ->next = fourth;
fourth ->data=4;
fourth ->next=NULL;
printf("Before insertion\n");
traversal(head);
head = insertatend(head,56);
printf("After insertion\n");
traversal(head);
return 0;
}
4.Insertion after a node:
Algorithm:
1.Create a new node 'previousnode' that is value of node.
2.Set ptr->next = previousnode->next & previousnode->next=ptr.
3.traverse all the nodes and print.
4.EXIT.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
void trv(struct node *ptr)
{
while (ptr != NULL)
{
printf(" Element is : %d\n", ptr->data);
ptr = ptr->next;
}
}
struct node *insertionafteranode(struct node *head,struct node * previousnode ,int data)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
ptr->data = data;
ptr->next = previousnode->next ;
previousnode->next = ptr;
return head;
}
int main()
{
struct node *head;
struct node *second;
struct node *third;
struct node *fourth;
head = (struct node *)malloc(sizeof(struct node *));
second = (struct node *)malloc(sizeof(struct node *));
third = (struct node *)malloc(sizeof(struct node *));
fourth = (struct node *)malloc(sizeof(struct node *));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = NULL;
printf("Before Insertion :\n");
trv(head);
head = insertionafteranode(head, second ,60);
printf("After insertion :\n");
trv(head);
return 0;
}


















