Bird
0
0
DSA Cprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head
Check if head is NULL?
YesList empty, nothing to delete
No
Move head to head->next
If new head not NULL, set new head->prev = NULL
Delete old head node
Done
Start at the head node, check if list is empty. If not, move head to next node, update pointers, delete old head.
Execution Sample
DSA C
void deleteFromBeginning() {
  if (head == NULL) return;
  Node* temp = head;
  head = head->next;
  if (head != NULL) head->prev = NULL;
  free(temp);
}
Deletes the first node of a doubly linked list by moving head and updating pointers.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with list10 <-> 20 <-> 30 <-> NULLhead → node with 10┌────────┐ ┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ data:30│ │ prev:∅ │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:∅ │ └────────┘ └────────┘ └────────┘ head
2Check if head is NULL10 <-> 20 <-> 30 <-> NULLhead not NULL, continueSame as step 1
3Set temp = head10 <-> 20 <-> 30 <-> NULLtemp → node with 10Same as step 1
4Move head to head->next20 <-> 30 <-> NULLhead → node with 20┌────────┐ ┌────────┐ │ data:20│ │ data:30│ │ prev:10│←── │ prev:20│←── │ next:30│──→ │ next:∅ │ └────────┘ └────────┘ head
5Set new head->prev = NULL20 <-> 30 <-> NULLhead->prev = NULL┌────────┐ ┌────────┐ │ data:20│ │ data:30│ │ prev:∅ │←── │ prev:20│←── │ next:30│──→ │ next:∅ │ └────────┘ └────────┘ head
6Free old head (node with 10)20 <-> 30 <-> NULLtemp freed┌────────┐ ┌────────┐ │ data:20│ │ data:30│ │ prev:∅ │←── │ prev:20│←── │ next:30│──→ │ next:∅ │ └────────┘ └────────┘ head
7End20 <-> 30 <-> NULLhead points to node with 20Final list state as step 6
💡 Execution stops after deleting the first node and updating head pointer.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
headnode with 10node with 10node with 20node with 20node with 20
tempNULLnode with 10node with 10node with 10freed
head->prevNULLNULLnode with 10NULLNULL
Key Moments - 3 Insights
Why do we set head->prev = NULL after moving head to next node?
Because the new head is now the first node, it should not have a previous node. This is shown in step 5 of the execution_table where head->prev changes from node with 10 to NULL.
What happens if the list is empty when we try to delete from beginning?
The function checks if head is NULL at step 2. If it is NULL, it returns immediately without doing anything, so no deletion occurs.
Why do we free the old head node after moving head pointer?
We free the old head to release memory and avoid memory leaks. This is done in step 6 after head moves to the next node.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of head after step 4?
ANode with data 10
BNode with data 20
CNULL
DNode with data 30
💡 Hint
Check the 'Pointer Changes' column at step 4 in execution_table.
At which step does head->prev get set to NULL?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table.
If the list was empty initially, what would happen at step 2?
Ahead moves to next node
Btemp is set to head
CFunction returns immediately
DOld head node is freed
💡 Hint
Refer to the 'Check if head is NULL' operation in execution_table step 2.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == NULL)
- Save current head in temp
- Move head to head->next
- If new head exists, set head->prev = NULL
- Free old head node
- List size decreases by one
- Handles empty list safely
Full Transcript
This visual trace shows how to delete the first node from a doubly linked list. We start by checking if the list is empty. If not, we save the current head node in a temporary pointer. Then we move the head pointer to the next node. If the new head is not null, we set its previous pointer to null because it is now the first node. Finally, we free the old head node to release memory. The execution table shows each step with the list nodes and pointer changes. The variable tracker follows the head and temp pointers and the head's previous pointer. Key moments clarify why we update head->prev and why we free the old node. The quiz tests understanding of pointer updates and empty list handling. This method safely removes the first node and updates the doubly linked list structure.