Showing posts with label Deletion Algorithm. Show all posts
Showing posts with label Deletion Algorithm. Show all posts

Deleting Node From 2 way linked List

Delete_Node_2WayList(List,Value, Start, End)


Here in this algorithm List is a 2 way linked list containing n nodes. Start and End are the pointers pointing the starting and ending nodes. We have to delete a node from the list which contains Value as its data. Ptr is an intermediate pointer that will be used to traverse the list.

[Checking if List is Empty]
Step 1: if Start=null then
                  return;
[Initializing the Ptr]
Step 2: Ptr = Start
[Searching for the Node which is to be deleted]
Step 3: While Ptr <> null and Ptr->Data <> Value repeat the step
                      i) Ptr = Ptr -> Next
[Checking if search was successful or not]
Step 4: if Ptr = null then
                return;
[Search was successful and Ptr is pointing to the node which is to be deleted]
Step 5:   i) Ptr->Prev->Next = Ptr->Next [Next of Previous node of current node will point the next of  current node] 
              ii) Ptr->Next->Prev = Ptr->Prev [Prev of Next node of current node will point the previous of current node]

[Now Current Node pointing by Ptr is not referenced from list anymore it can be freed now]
             iii) FREE (Ptr)

Step 6: End;

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;


Algorithm: To remove element from Array

Algorithm: RemoveElementFromArray(rIndex,valArray[],n,removedData)

In this algorithm rIndex is index from which element is to be removed and n is the size of array valArray. removedData will contain the value which is removed from array from index rIndex.
*consider lower bound of array is 1.

Step 1: [Check for size of array]

           if rIndex > n then

                 a) raise_error "Array Index out of bound"
                 b) Exit
Step 2: removedData = valArray[rIndex]

Step 3: [Shifting elements of array]
           Declare index as integer
           i) for index=rIndex to n-1 repeat step a to b

               a) valArray[index] = valArray[index+1]
               b) index= index+ 1

           ii) valArray[index] = null

Step 4: End


Explanation