/*PROGRAM TO INSERT AN ELEMENT AFTER A SPECIFIED ELEMENT IN THE LINKED LIST*/
#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node *start=NULL,*ptr,*temp,*ptr1,*ptr2;
int num,c=0,item;
char ch;
clrscr();
cout<<“Inserting elements in the list:”<<endl;
do
{
temp=new node;
if(start==NULL)
{
cout<<“Enter the first element: “;
cin>>num;
temp->data=num;
temp->link=start;
start=temp;
cout<<“Elements of list are:”<<endl;
cout<<start->data;
}
else if(start->link==NULL)
{
cout<<“Enter second element: “;
cin>>num;
temp->data=num;
start->link=temp;
temp->link=NULL;
cout<<“Elements of list are:”<<endl;
cout<<start->data<<“\t”;
cout<<start->link->data;
}
else
{
cout<<“Enter element after which you want to insert new element: “;
cin>>item;
cout<<“Enter element: “;
cin>>num;
temp->data=num;
for(ptr=start;ptr!=NULL;ptr=ptr->link)
{
if(ptr->data==item)
{
temp->link=ptr->link;
ptr->link=temp;
c=1;
}
}
if(c==1)
{
cout<<“Elements of list are:”<<endl;
for(ptr=start;ptr!=NULL;ptr=ptr->link)
{
cout<<ptr->data<<“\t”;
}
c=0;
}
else
cout<<“!!Enter a valid choice!!”<<endl;
}
cout<<endl<<“Do you want to continue?? “;
cin>>ch;
}while(ch==’y’||ch==’Y’);
cout<<“Elements in the list are:”<<endl;
for(ptr=start;ptr!=NULL;ptr=ptr->link)
{
cout<<ptr->data<<“\t”;
}
getch();
}