Algorithm to delete a Node from Linked List

Delete_Node_LL(List, Start, value)

Here, List is a Linked list Containing n Nodes.Start is a pointer pointing to the starting node of the List We have to delete a node which contains value as its data. Ptr is an intermediate pointer, which will be used to traverse the List. Prev is a intermediate pointer which will be contains the address of Node which is previous to the node pointing by Ptr


Step 1: [Initializing the pointers Ptr will be pointing the starting node and Prev will be null]
             Ptr = Start, Prev = null

Step 2: [If Node to be deleted is 1st node]
            if Ptr->Data = value then
                  i) Start = Ptr->Next
                  ii) Free(Ptr) [Releasing memory consumed by node to be deleted]
                  iii) Return; 

Step 3:[Since node to be deleted is not starting node so moving Ptr to 2nd node and Prev will be pointing starting node] 
           i)Prev = Ptr
          ii)Ptr = Ptr->Next

Step 4: Repeat Step 4(i) to 4(iv) till Ptr is not null. 
           i) if Ptr->Data = value then
                         a) Prev->Next = Ptr->Next; 
                         b) Free(Ptr); 
                         c) Return;
          ii) Prev = Ptr;
         iii) Ptr = Ptr ->Next

Step 5: [Node was not found raise error or return the control]
            Return;



Click Here for Explanation

No comments:

Post a Comment