Bird
0
0
DSA Cprogramming~10 mins

Delete by Value in Doubly Linked List in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Delete by Value in Doubly Linked List
Start at head
Check if current node's value == target?
NoMove to next node
If current == null
Adjust pointers of prev and next nodes
Value not found, stop
Delete current node
Stop
Start from the head, look for the node with the target value. If found, adjust the previous and next nodes' pointers to remove it, then delete the node.
Execution Sample
DSA C
void deleteByValue(Node** head, int val) {
  Node* current = *head;
  while (current != NULL && current->data != val) {
    current = current->next;
  }
  if (current == NULL) return;
  if (current->prev) current->prev->next = current->next;
  else *head = current->next;
  if (current->next) current->next->prev = current->prev;
  free(current);
}
This code searches for a node with the given value and deletes it from the doubly linked list by adjusting pointers.
Execution Table
StepOperationCurrent Node ValuePointer ChangesVisual State
1Start at head1None1 <-> 2 <-> 3 <-> 4 <-> NULL
2Check if current->data == 31None1 <-> 2 <-> 3 <-> 4 <-> NULL
3Move to next node2None1 <-> 2 <-> 3 <-> 4 <-> NULL
4Check if current->data == 32None1 <-> 2 <-> 3 <-> 4 <-> NULL
5Move to next node3None1 <-> 2 <-> 3 <-> 4 <-> NULL
6Check if current->data == 33None1 <-> 2 <-> 3 <-> 4 <-> NULL
7Adjust pointers to remove node 332->next = 4, 4->prev = 21 <-> 2 <-> 4 <-> NULL
8Delete node 33Node 3 freed1 <-> 2 <-> 4 <-> NULL
9StopNULLNone1 <-> 2 <-> 4 <-> NULL
💡 Node with value 3 found and deleted; list updated accordingly.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
currentNode(1)Node(2)Node(3)Node(3)NULL
headNode(1)Node(1)Node(1)Node(1)Node(1)
list size4 nodes4 nodes4 nodes3 nodes3 nodes
Key Moments - 3 Insights
Why do we check if current->prev is NULL before changing pointers?
Because if current->prev is NULL, it means current is the head node. We must update the head pointer instead of current->prev->next. This is shown in step 7 where pointer changes depend on whether current->prev exists.
What happens if the value to delete is not found in the list?
The loop ends with current == NULL (step 9), and the function returns without changes. The list remains the same, as no pointer adjustments or deletions occur.
Why do we update both prev and next pointers when deleting a node?
Because in a doubly linked list, each node points to both previous and next nodes. To remove a node cleanly, we must link its previous node to its next node and vice versa, as shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'current' after step 5?
ANode with value 3
BNode with value 2
CNode with value 4
DNULL
💡 Hint
Check the 'Current Node Value' column at step 5 in the execution_table.
At which step do the pointers get adjusted to remove the node?
AStep 6
BStep 7
CStep 8
DStep 9
💡 Hint
Look for 'Adjust pointers' operation in the execution_table.
If the node to delete was the head (value 1), what pointer would be updated?
Acurrent->prev->next
Bhead pointer
Ccurrent->next->prev
DNo pointer changes
💡 Hint
Refer to key_moments about updating head when current->prev is NULL.
Concept Snapshot
Delete by Value in Doubly Linked List:
- Start at head, traverse nodes until value found
- If node found:
  - If prev exists, prev->next = current->next
  - Else update head to current->next
  - If next exists, next->prev = current->prev
- Free the node
- If not found, do nothing
Full Transcript
To delete a node by value in a doubly linked list, start from the head and move through nodes until you find the node with the target value. If the node is found, adjust the previous node's next pointer and the next node's previous pointer to bypass the current node. If the node is the head, update the head pointer. Finally, free the memory of the deleted node. If the value is not found, the list remains unchanged.