Insertion of a Node:
One of the most primitive operations that can be done in a singly linked list is
the insertion of a node. Memory is to be allocated for the new node (in a
similar way that is done while creating a list) before reading the data. The
new node will contain empty data field and empty next field. The data field
of the new node is then stored with the information read from the user. The
next field of the new node is assigned to NULL. The
new node can then be inserted at three different placesnamely:
Inserting a node at the beginning.
Inserting a node at the end.
Inserting a node at intermediate position.
Inserting a node at the beginning:
The following steps are to be followed to insert a new node at the beginning
of the list:
Get the new node using getnode().
newnode = getnode();
If the list is empty then start = newnode.
If the list is not empty, follow the steps given below:
newnode -> next = start;
start = newnode;
The function insert_at_beg(value), is used for inserting a node at the
beginning
void insert_at_beg(int value)
Struct node *newnode;
newnode = create(value);
if(start == NULL)
start = newnode;
}
else
newnode -> next = start;
start = newnode;
Inserting a node at the end:
The following steps are followed to insert a new node at the end of the list:
Get the new node using create(value)
newnode = getnode();
If the list is empty then start = newnode.
If the list is not empty follow the steps given below:
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
function insert_at_end(), is used for inserting a node at the end.
void insert_at_end(int value)
Struct node *newnode, *temp;
Newnewnode=create(value);
if(start == NULL)
start = newnode;
else
{
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
Inserting a node at intermediate position:
The following steps are followed, to insert a new node in an intermediate
position in the list:
Get the new node using getnode().
newnode = getnode();
Ensure that the specified position is in between first node and last node. If
not, specified position is invalid. This is done by countnode() function.
Store the starting address (which is in start pointer) in temp and prev
pointers. Then traverse the temp pointer upto the specified position followed
by prev pointer.
After reaching the specified position, follow the steps given below:
prev -> next = newnode;
newnode -> next = temp;
Let the intermediate position be 3.
Figure 3.2.7 shows inserting a node into the single linked list at a specified
intermediate position other than beginning and end.
function insert_at_mid(), is used for inserting a node in the intermediate
position.
void insert_at_mid(int value,int pos)
Struct node *newnode, *temp, *temp2;
int I, c= 1;
newnode = create(value);
i=count();
If(pos==1)
Insert_at_begin(value);
else if( pos >i+1)
Printf(“insertion is not possible “);
Return;
Else
temp1=head;
while (c<=pos-1 && temp!=NULL)
temp2 = temp1;
temp 1= temp 1-> next;
c++;
newnode-> next = temp2->next;
temp->next=newnode;