Deletion in Circular doubly linked list at beginning
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 last node of the list. The first node of the list i.e. pointed by head pointer, will need to be deleted. Therefore the last node must contain the address of the node that is pointed by the next pointer of the existing head node. Use the following statement for this purpose.
The new head node i.e. next of existing head node must also point to the last node of the list through its previous pointer. Use the following statement for this purpose.
Now, free the head pointer and the make its next pointer, the new head node of the list.
in this way, a node is deleted at the beginning from a circular doubly linked list.
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 -> NEXT = HEAD -> NEXT
- Step 6: SET HEAD -> NEXT -> PREV = TEMP
- Step 7: FREE HEAD
- Step 8: SET HEAD = TEMP -> NEXT
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
C Function
Output
1.Append List 2.Delete Node from beginning 3.Exit 4.Enter your choice?1 Enter the item 12 Node Inserted 1.Append List 2.Delete Node from beginning 3.Exit 4.Enter your choice?2 Node Deleted