Bird
0
0
DSA Cprogramming~10 mins

Delete from End of Doubly Linked List in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Delete from End of Doubly Linked List
Start at tail node
Is tail null?
YesList empty, nothing to delete
No
Move tail to tail.prev
Set new tail.next to null
Delete old tail node
Done
Start from the tail node, check if list is empty, move tail pointer back, update links, and delete the last node.
Execution Sample
DSA C
void deleteFromEnd(Node** head, Node** tail) {
    if (*tail == NULL) return;
    Node* toDelete = *tail;
    *tail = (*tail)->prev;
    if (*tail) (*tail)->next = NULL;
    else *head = NULL;
    free(toDelete);
}
Deletes the last node from a doubly linked list by updating tail and head pointers and freeing memory.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with list 10 <-> 20 <-> 30 (tail=30)10 <-> 20 <-> 30head → 10, tail → 30┌────────┐ ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ data:30│ │ prev:∅ │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:∅ │ └────────┘ └────────┘ └────────┘ head tail
2Check if tail is NULL (No)10 <-> 20 <-> 30No pointer changeSame as step 1
3Set toDelete = tail (30)10 <-> 20 <-> 30toDelete → 30Same as step 1
4Move tail to tail.prev (20)10 <-> 20 <-> 30tail: 30 → 20┌────────┐ ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ data:30│ │ prev:∅ │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:∅ │ └────────┘ └────────┘ └────────┘ head tail (moved) toDelete
5Set tail.next = NULL10 <-> 2020.next: 30 → NULL┌────────┐ ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ data:30│ │ prev:∅ │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:∅ │ │ next:∅ │ └────────┘ └────────┘ └────────┘ head tail toDelete
6Free toDelete (30)10 <-> 20toDelete removed┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ prev:∅ │←── │ prev:10│ │ next:20│──→ │ next:∅ │ └────────┘ └────────┘ head tail
7Done10 <-> 20head → 10, tail → 20Final list after deletion
💡 Deletion complete; tail updated and last node removed
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
head101010101010
tail303020202020
toDeleteNULL303030NULLNULL
Key Moments - 3 Insights
Why do we move tail to tail.prev before deleting the node?
Because we need to update the tail pointer to the new last node before removing the old tail. See step 4 in execution_table where tail moves from 30 to 20.
What happens if the list has only one node?
After deletion, both head and tail become NULL. This is handled in the code by setting head to NULL if tail becomes NULL (not shown in this example but important).
Why do we set tail.next to NULL after moving tail?
To disconnect the new tail from the deleted node, ensuring the list ends properly. See step 5 where 20.next changes from 30 to NULL.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the new value of tail?
ANode with data 30
BNode with data 20
CNULL
DNode with data 10
💡 Hint
Check the 'Pointer Changes' column at step 4 in execution_table
At which step does the tail's next pointer become NULL?
AStep 6
BStep 3
CStep 5
DStep 2
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table
If the list had only one node, what would happen to head after deletion?
AIt becomes NULL
BIt points to tail
CIt remains pointing to the deleted node
DIt points to itself
💡 Hint
Refer to key_moments about single node list behavior
Concept Snapshot
Delete from End of Doubly Linked List:
- Start at tail node
- If tail is NULL, list is empty
- Move tail pointer to tail.prev
- Set new tail.next to NULL
- Free old tail node
- Update head to NULL if list becomes empty
Full Transcript
To delete the last node from a doubly linked list, we start at the tail node. If the tail is NULL, the list is empty and nothing is done. Otherwise, we save the tail node to delete, move the tail pointer back to the previous node, set the new tail's next pointer to NULL to disconnect the old tail, then free the old tail node. If the list had only one node, after deletion both head and tail become NULL. This process ensures the list remains properly linked and updated after deletion.