Deletion in singly linked list after the specified node :
In order to delete the node, which is present after the specified node, we need to skip the desired number of nodes to reach the node after which the node will be deleted. We need to keep track of the two nodes. The one which is to be deleted the other one if the node which is present before that node. For this purpose, two pointers are used: ptr and ptr1.
Use the following statements to do so.
Now, our task is almost done, we just need to make a few pointer adjustments. Make the next of ptr1 (points to the specified node) point to the next of ptr (the node which is to be deleted).
This will be done by using the following statements.
Algorithm
- STEP 1: IF HEAD = NULL
- STEP 2: SET TEMP = HEAD
- STEP 3: SET I = 0
- STEP 4: REPEAT STEP 5 TO 8 UNTIL I
- STEP 5: TEMP1 = TEMP
- STEP 6: TEMP = TEMP → NEXT
- STEP 7: IF TEMP = NULL
- STEP 8: I = I+1
- STEP 9: TEMP1 → NEXT = TEMP → NEXT
- STEP 10: FREE TEMP
- STEP 11: EXIT
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
WRITE “DESIRED NODE NOT PRESENT”
GOTO STEP 12
END OF IF
END OF LOOP
C function
Output
1.Append List 2.Delete node 3.Exit 4.Enter your choice?1 Enter the item 12 Node inserted 1.Append List 2.Delete node 3.Exit 4.Enter your choice?1 Enter the item 23 Node inserted 1.Append List 2.Delete node 3.Exit 4.Enter your choice?2 12 There are less than 12 elements in the list.. 1.Append List 2.Delete node 3.Exit 4.Enter your choice?2 1 Deleted 1 node