## Insert New Node After Given Node in Linked List

In order to insert a new node after given node in the linked list we have to follows the below steps:
(1) First we have to check weather free node is available in the Availability Stack or not. If free node is available then we can allocate memory to new node.
(2) After creating new node we have to check weather linked list is empty or not. We have two possibilities:
(A) Linked List is empty (FIRST=NULL). Hence list is empty, specified node is not found in the linked list. In this case we can not insert new node after given node.
(B) Linked List is not empty (FIRST ≠ NULL). In this case we have to traverse from first node to last node in the list until given node is found. If node is found in the list then we can insert new node after that node otherwise we can not insert new node after given node.

## Algorithm to Insert New Node After Given Node

Step 1: If AVAIL=NULL then
Write “Availability Stack is Empty”
Else
NEW_NODE=AVAIL
Step 2: If FIRST = NULL then
Else
NEW_NODE -> INFO = VALUE
SAVE = FIRST
Repeat while X ≠ SAVE->INFO and SAVE->LINK ≠ NULL
PRED = SAVE
If X = SAVE->INFO then
Else
Step 3: Exit

## Program to Insert New Node After Given Node

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
};
struct node *FIRST;
void createlist()
{
char ch;
printf("Enter n for break\n");
ch=getchar();
while(ch!='n')
{
struct node *NEW_NODE,*SAVE;int x;
NEW_NODE=(struct node *)malloc(sizeof(struct node));
printf("Enter Data:");
scanf("%d",&x);
NEW_NODE->info=x;
if(FIRST==NULL)
{
FIRST=NEW_NODE;
}
else
{
SAVE=FIRST;
{
}
}
fflush(stdin);
printf("Enter n for break\n");
ch=getchar();
}
}
void insertafter(int x)
{
struct node *NEW_NODE,*SAVE,*PRED;
int value;
NEW_NODE=(struct node *)malloc(sizeof(struct node));
printf("Enter value of node:");
scanf("%d",&value);
NEW_NODE->info=value;
if(FIRST==NULL)
{
}
else
{
SAVE=FIRST;
{
PRED=SAVE;
}
if(SAVE->info==x)
{
}
else
{
}
}
}
void display()
{
struct node *SAVE;
if(FIRST==NULL)
{
printf("Linked List is empty\n");
return;
}
printf("elements are:\n");
SAVE=FIRST;
while(SAVE!=NULL)
{
printf("|%d|",SAVE->info);
else
printf("|%d|->",SAVE->info);
}
printf("\n");
return;
}
void main()
{
int x;
clrscr();
FIRST=NULL;
createlist();
display();
printf("Enter value of Node after which you want to enter new node:");
scanf("%d",&x);
insertafter(x);
display();
getch();
}