Insert Node
Insert Node
#include <iostream>
#include <string>
using namespace std;
struct node
{
string name;
int age;
node* next;
}*p,*h;
void insertb(int n)
{
struct node *pos;
int i=1;
for(pos=h;pos->next!=NULL;pos=pos->next)
i++;
if(i<n)
{
cout<<"enter valid position"<<endl;
}
else
{
for(pos=h,i=1;(pos->next!=NULL && i<n-1);pos=pos->next,i++);
p=new node();
input(p);
p->next=pos->next;
pos->next=p;
}
}
int main()
{
struct node *pos;
int no,x;
cout<<"enter no of intial nodes to create"<<endl;
cin>>no;
h=new node();
input(h);
for(int i=2;i<=no;i++)
{
cout<<"Enter data to node no :"<<i<<endl;
p=new node();
input(p);
if(i==2)
h->next=p;
else
{
for(pos=h;pos->next!=NULL;pos=pos->next);
pos->next=p;
}
}
cout<<"\nLinked list"<<endl;
for(pos=h;pos!=NULL;pos=pos->next)
{
cout<<"-> "<<pos->name<<" "<<pos->age<<endl;
}