Program in c++ to insert a node at the middle of linked list.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
}*start;
class single_llist
{
public:
node*create_node(int);
void insert_begin();
void insert_pos();
void display();
single_llist()
{
Start=NULL;
}
};
main()
{
int choice,nodes,element,position,i;
single_llist sl;
start=NULL;
clrscr();
while(1)
{
cout<<"\n
-------------------------------";
cout<<"\n Operation on single
linked list";
cout<<"\n
-------------------------------";
cout<<"\n 1.Insert nodes at
beginning";
cout<<”\n 2.Insert node at given
position”;
cout<<"\n 3.Display linked
list";
cout<<"\n 4.Exit";
cout<<"\n Enter your
choice:";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n Inserting
nodes at Beginning:";
sl.insert_begin();
break;
case 2:cout<<Insert node at given
position”;
sl.insert_pos();
break;
case 3:cout<<"\n Display
elements of linked list";
sl.display();
break;
case 4:cout<<"\n
Exit...";
exit(1);
break;
default:cout<<"\n Wrong
choice";
}
}
}
node*single_llist::create_node(int value)
{
struct node*temp,*s;
temp=new(struct node);
if(temp==NULL)
{
cout<<"\n Memory not
allocated";
return 0;
}
else
{
temp->info=value;
temp->next=NULL;
return temp;
}
}
void single_llist::insert_begin()
{
int value;
cout<<"\n Enter the value to be
inserted:";
cin>>value;
struct node*temp,*p;
temp=create_node(value);
if(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
p=start;
start=temp;
start->next=p;
}
cout<<"\n Element inserted at
beginning";
}
void single_llist::insert_pos()
{
int value,pos,counter=0;
cout<<”\n Enter the value to be
inserted”;
cin>>value;
struct node*temp,*s,*ptr;
temp=create_node(value);
cout<<”\n Enter the position at which
node to be inserted”;
cin>>pos;
int I;
s=start;
while(s!=NULL)
{
s==s->next;
counter;
}
If(pos==1)
{
If(start==NULL)
{
start=temp;
start->next=NULL;
}
else
{
ptr=start;
start=temp;
start->next=ptr;
}
}
Else
if(pos>1 && pos<=counter)
{
s=start;
for(i=1;i<pos;i++)
{
ptr=s;
s=s->next;
}
ptr->next=temp;
temp-next=s;
}
else
cout<<”\n Position out of range”;
}
void single_llist::display()
{
struct node*temp;
if(start==NULL)
{
cout<<"\n The list is
empty"<<endl;
return;
}
temp=start;
cout<<"\n Element of list
are:";
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->next;
}
cout<<"NULL";
}
/* OUTPUT:
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert node at beginning
2.Insert node at given position
3.Display linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:10
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at given position
3.Display
linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:20
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert node at given position
3.Display linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:30
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert node at given position
3.Display linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:40
Element inserted at beginning
----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at given position
3.Display
linked list
4.Exit
Enter your choice:1
Inserting nodes at Beginning:
Enter the value to be inserted:50
Element inserted at beginning
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at given position
3.Display
linked list
4.Exit
Enter your choice:2
Insert node
at given position
Enter the
value to be insertrd 35
Enter the
position at which node to be inserted 3
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert
node at given position
3.Display
linked list
4.Exit
Enter your choice:3
Display elements of linked list
Element of list are:50->40-> 35->30->20->10->NULL
-----------------------------------------
Operation on single linked list
-----------------------------------------
1.Insert nodes at beginning
2.Insert node at given position
3.Display
linked list
4.Exit
Enter your choice:4
Exit...