Hello Friends , Welcome Back to Tech Fantastik . Today We are going Learn How to insert Node in Linked List at a given position . As we Know Linked List is very main part of Data Structure . Question related to Linked List are Frequently asked in interviews. So here I am Providing you the Code written in simple format Related To the insertion operations in Linked List .The Bonus Tip is that , I also have given function code of Traversal of linked List because after inserting the node we have to check it out that insertion is done or not.
Insertion Operations in Linked List
case 1: Linked List Insertion at The Begining
case 2: Insertion At index in Linked List
case 3: Insertion Of Node At the Ending Of Linked List
case 4: Insertion After Given Node
//Operation on Linked List
#include <stdio.h>
#include <stdlib.h>
// Creating Structure Of Linked List
struct Node{
int data;
struct Node *nxt;
};
// Linked List Traversal Function
void Trav(struct Node *ptr){
while(ptr!=NULL)
{ printf("Element is : %d \n",ptr->data);
ptr=ptr->nxt;
}}
// Linked List Insertion at The Begining of Linked List
struct Node *Insertfirst(struct Node *head , int data){
struct Node *ptr = (struct Node*)malloc(sizeof(struct Node));
ptr-> data = data;
ptr -> nxt = head ;
return ptr;
}
// Insertion At index in Linked List
struct Node * insertAtIndex(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 -> nxt;
i++;
}
// Ahead written code [ Rearange = Error]
ptr -> data = data;
ptr -> nxt = p -> nxt;
p -> nxt = ptr;
return head;
}
// Insertion Of Node At the Ending Of Linked List
struct Node * insertAtEnd(struct Node *head , int data)
{
struct Node *ptr = (struct Node*)malloc(sizeof(struct Node));
struct Node *p = head;
while(p->nxt != NULL)
{
p = p -> nxt;
}
ptr -> data = data;
p -> nxt = ptr;
ptr -> nxt = NULL;
return head;
}
// Insertion After Given Node
struct Node *insertAfterNode(struct Node *head , int data ,struct Node *gnod)
{ struct Node *ptr = (struct Node*)malloc(sizeof(struct Node));
ptr -> data = data;
ptr -> nxt = gnod -> nxt;
gnod -> nxt = ptr;
return head;
}
int main() {
struct Node *e1 ,*e2 ,*e3 , *e4;
e1 = (struct Node*)malloc(sizeof(struct Node));
e2 = (struct Node*)malloc(sizeof(struct Node));
e3 = (struct Node*)malloc(sizeof(struct Node));
e4 = (struct Node*)malloc(sizeof(struct Node));
e1->data = 5;
e1->nxt = e2;
e2->data = 4;
e2->nxt = e3;
e3->data = 8;
e3->nxt = e4;
e4->data = 10;
e4->nxt = NULL;
printf("\n Linked List Traversal \n\n");
Trav(e1);
e1 = Insertfirst(e1 , 68);
printf("\nAfter Inserting In Begining \n\n");
Trav(e1);
printf("\nAfter Insertion After Node \n\n");
e1 = insertAtIndex(e1,3,2);
Trav(e1);
printf("\nAfter Insertion Node in The End \n\n");
insertAtEnd(e1 , 800 );
Trav(e1);
printf("\nAfter Insertion Node After the Given Node \n\n");
insertAfterNode(e1 , 2999 , e3);
Trav(e1);
return 0;
}
Output of the Following Program :
Linked List Traversal
Element is : 5
Element is : 4
Element is : 8
Element is : 10
After Inserting In Begining
Element is : 68
Element is : 5
Element is : 4
Element is : 8
Element is : 10
After Insertion After Node
Element is : 68
Element is : 5
Element is : 3
Element is : 4
Element is : 8
Element is : 10
After Insertion Node in The End
Element is : 68
Element is : 5
Element is : 3
Element is : 4
Element is : 8
Element is : 10
Element is : 800
After Insertion Node After the Given Node
Element is : 68
Element is : 5
Element is : 3
Element is : 4
Element is : 8
Element is : 2999
Element is : 10
Element is : 800
I hope you understood the code of insertion of node in given linked List . If you find this helpful then share this code among your friends.