You are on page 1of 3

#include<conio.

h>
#include<iostream.h>
struct node
{
int info;
node *link;
};
void main()
{
clrscr();
node *START,*ptr,*ptr1;
int n,i,item,loc,count=0;
cout<<"Enter total number of nodes : ";
cin>>n;
START = new node;
ptr = START;
for(i=1;i<=n;i++)
{
cout<<"Enter value of node number "<<i<<" = ";
cin>>ptr->info;
if(i==n)
ptr->link = NULL;
else
ptr->link = new node;

ptr = ptr->link;
}
cout<<"\n\nEntered linked list is - \n";
ptr = START;
while(ptr!=NULL)

{
cout<<ptr->info<<"\t";
ptr = ptr->link;
}
cout<<"Enter the new value";
cin>>item;
ptr1=new node;
ptr1->info= item;
//ptr1->link=ptr;
//ptr=ptr1;
//ptr1->link=START;
//START=ptr1;
cout<<"enter the location after which u wanna insert node";
cin>>loc;
ptr = START;
count=1;
while(ptr!=NULL)
{
if(count==loc)
break;
ptr = ptr->link;
++count;
}
if(ptr==NULL)
cout<<"element "<<loc<<" not present in list ";
else
{
ptr1->link=ptr->link;
ptr->link=ptr1;
}

cout<<"\n\nNew linked list is - \n";


ptr = START;
while(ptr!=NULL)
{
cout<<ptr->info<<"\t";
ptr = ptr->link;
}

getch();
}

You might also like