Insert node at any Position : Singly Linked List - Data Structure

Algorithm for Insert node
  • Step1: First check the head status, if head status is NULL then it is EMPTY LIST, so no need to perform any task.
  • Step2: If head status is other then NULL then,
        • Get node count value.
        • Get node position value to perform insert operation.
        • validate node position value, if valid position, then go for further process.
  • Step3: If node position value is 1.Then process following steps
        • Create a new node and store address in temporary head i.e. th1.
        • Read node information and store in th1->data.
        • Assign current head value to newly created node to next i.e. th1->next=head.
        • shift head to newly create node position i.e. head=th1.  
  • Step3: If node position value is other then 1, Then process following steps
        • Take a temporary head i.e. th1 and assign current head value to th1.
        • Create a loop and travel up to link position -1.
        • Create a new node and store address in temporary head i.e. th2.
        • Read node information and store data in th2->data.
        • Assign th1->next value to th2->next, i.e. previous node next address to newly created node next value.
 
Video for Insert


No comments:

Post a Comment