Previous Article: Inserting a Node in Doubly Linked List.
Deletion of a Node
Let us say our current Doubly Linked List is as shown in Figure-1.Figure 1: Current Doubly Linked List |
- Deleting First Node of the List Now we have to delete First Node from the List shown in Figure-1. Because of this operation HEAD and Node-2 are affected.
- In HEAD - FIRST variable should now point to NODE-2 (i.e HEAD->FIRST = HEAD->FIRST->NEXT). If you see HEAD->FIRST->NEXT actually HEAD->FIRST currently points to NODE-1 so HEAD->FIRST->NEXT is equivalent to NODE-1->NEXT which is NODE-2.
- In NODE-2 - NEXT variable remains unchanged and PREV variable should now point to NULL since it is the first Node in the List.
- Decrement LENGTH variable in HEAD so that it maintains proper count of Nodes in the List.
- Pseudocode:
HEAD->FIRST = HEAD->FIRST->NEXT NODE-2->PREV = NULL decrement(HEAD->LENGTH)Output:
Figure 2: After deleting the First Node in the List. |