Write an Algorithm to implement insertion operations ( insert at front, Insert at End, Insert at Middle) on a linked list.

Algorithm to insert node at front: 
Insert at front()
{
Allocate a memory for new node(new1)
if (new1== NULL)
then
display memory is full (insertion is not possible)
end if
else
read element ele;
new1->data=ele;/*copy the data into new1 node */
new1->next=header->next;
header->next=new1;
end else
}

Algorithm to insert node at End: 
Insert at End()
{
Allocate a memory for new node(new1)
if(new1== NULL)
then
display memory is full (insertion is not possible)
end if
else
ptr=header
while(ptr->next!=NULL )/*ptr moves end of list */
then
ptr=ptr->next
end
new1->next=NULL
new1->data=ele
ptr->next=new1
end else
}

Algorithm to insert node at Middle:
Insert at Middle()
{
Allocate a memory for new node(new1)
if (new1== NULL)
then
display memory is full (insertion is not possible)
exit
else
read ele,pos
ptr=header
count=0
while(ptr->next!=NULL)
{
ptr=ptr->next;
count ++;
}
if(count<pos-1)
then
display position is not of range
end
ptr=header
count=0
while(count<pos-1)
then
ptr=ptr->next
count++
end
new1->data=ele
new1->next=ptr->next
ptr->next=new1
end else
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: