Deletion in circular doubly linked list at end
There can be two scenario of deleting the first node in a circular doubly linked list.
The node which is to be deleted can be the only node present in the linked list. In this case, the condition head → next == head will become true, therefore the list needs to be completely deleted.
It can be simply done by assigning head pointer of the list to null and free the head pointer.
in the second scenario, the list contains more than one element in the list, therefore the condition head → next == head will become false. Now, reach the last node of the list and make a few pointer adjustments there. Run a while loop for this purpose
Now, temp will point to the node which is to be deleted from the list. Make the next pointer of previous node of temp, point to the head node of the list.
make the previous pointer of the head node, point to the previous node of temp.
Now, free the temp pointer to free the memory taken by the node.
in this way, the last node of the list is deleted.
Algorithm
- Step 1: IF HEAD = NULL
- Step 2: SET TEMP = HEAD
- Step 3: Repeat Step 4 while TEMP -> NEXT != HEAD
- Step 4: SET TEMP = TEMP -> NEXT
- Step 5: SET TEMP -> PREV -> NEXT = HEAD
- Step 6: SET HEAD -> PREV = TEMP -> PREV
- Step 7: FREE TEMP
- Step 8: EXIT
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
C Function
Output
1.Append List 2.Delete Node from last 3.Exit 4.Enter your choice?1 Enter the item 12 Node Inserted 1.Append List 2.Delete Node from last 3.Exit 4.Enter your choice?2 Node Deleted