Bird
0
0
DSA Cprogramming~10 mins

Insert at Beginning of Doubly Linked List in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at Beginning of Doubly Linked List
Create new node with data
Set new_node.prev = NULL
Set new_node.next = head
If head != NULL?
NoSkip
|Yes
Set head.prev = new_node
Set head = new_node
Done
This flow shows how a new node is created and inserted at the start of the doubly linked list, updating pointers carefully.
Execution Sample
DSA C
struct Node {
  int data;
  struct Node* prev;
  struct Node* next;
};

void insertAtBeginning(struct Node** head, int data) {
  struct Node* new_node = malloc(sizeof(struct Node));
  new_node->data = data;
  new_node->prev = NULL;
  new_node->next = *head;
  if (*head != NULL) (*head)->prev = new_node;
  *head = new_node;
}
This code creates a new node and inserts it at the beginning of a doubly linked list, updating the head pointer.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10Emptynew_node allocated
2Set new_node.prev = NULLnew_node(10)new_node.prev = NULL┌────────┐ │ data:10│ │ prev:∅ │ │ next:∅ │ └────────┘
3Set new_node.next = head (NULL)new_node(10)new_node.next = NULL┌────────┐ │ data:10│ │ prev:∅ │ │ next:∅ │ └────────┘
4Check if head != NULL (head is NULL)new_node(10)No change┌────────┐ │ data:10│ │ prev:∅ │ │ next:∅ │ └────────┘
5Set head = new_nodenew_node(10)head → new_nodehead ┌────────┐ │ data:10│ │ prev:∅ │ │ next:∅ │ └────────┘
6Insert 20 at beginningnew_node(10)Create new_node(20)head → 10
7Set new_node.prev = NULL20 → 10new_node(20).prev = NULL┌────────┐ ┌────────┐ │ data:20│ │ data:10│ │ prev:∅ │ │ prev:? │ │ next:? │ │ next:∅ │ └────────┘ └────────┘
8Set new_node.next = head (10)20 → 10new_node(20).next = 10┌────────┐ ┌────────┐ │ data:20│ ──→│ data:10│ │ prev:∅ │ │ prev:? │ │ next:──┼ │ next:∅ │ └────────┘ └────────┘
9Set head.prev = new_node (10.prev = 20)20 → 1010.prev = 20┌────────┐ ┌────────┐ │ data:20│ ──→│ data:10│ │ prev:∅ │ │ prev:──┼ │ next:──┼ │ next:∅ │ └────────┘ └────────┘
10Set head = new_node (head → 20)20 → 10head → 20head ┌────────┐ ┌────────┐ │ data:20│ ──→│ data:10│ │ prev:∅ │ │ prev:──┼ │ next:──┼ │ next:∅ │ └────────┘ └────────┘
11Insert 30 at beginning20 → 10Create new_node(30)head → 20
12Set new_node.prev = NULL30 → 20 → 10new_node(30).prev = NULL┌────────┐ ┌────────┐ ┌────────┐ │ data:30│ │ data:20│ │ data:10│ │ prev:∅ │ │ prev:? │ │ prev:? │ │ next:? │ │ next:? │ │ next:∅ │ └────────┘ └────────┘ └────────┘
13Set new_node.next = head (20)30 → 20 → 10new_node(30).next = 20┌────────┐ ──→ ┌────────┐ ┌────────┐ │ data:30│ │ data:20│ │ data:10│ │ prev:∅ │ │ prev:? │ │ prev:? │ │ next:──┼ │ next:? │ │ next:∅ │ └────────┘ └────────┘ └────────┘
14Set head.prev = new_node (20.prev = 30)30 → 20 → 1020.prev = 30┌────────┐ ──→ ┌────────┐ ┌────────┐ │ data:30│ │ data:20│ │ data:10│ │ prev:∅ │ │ prev:──┼ │ prev:? │ │ next:──┼ │ next:? │ │ next:∅ │ └────────┘ └────────┘ └────────┘
15Set head = new_node (head → 30)30 → 20 → 10head → 30head ┌────────┐ ──→ ┌────────┐ ┌────────┐ │ data:30│ │ data:20│ │ data:10│ │ prev:∅ │ │ prev:──┼ │ prev:? │ │ next:──┼ │ next:? │ │ next:∅ │ └────────┘ └────────┘ └────────┘
16End of insertions30 → 20 → 10No changehead ┌────────┐ ──→ ┌────────┐ ┌────────┐ │ data:30│ │ data:20│ │ data:10│ │ prev:∅ │ │ prev:──┼ │ prev:──┼ │ next:──┼ │ next:──┼ │ next:∅ │ └────────┘ └────────┘ └────────┘
💡 All nodes inserted at beginning; head updated each time; prev and next pointers set correctly.
Variable Tracker
VariableStartAfter Step 5After Step 10After Step 15Final
headNULLNode(10)Node(20)Node(30)Node(30)
new_nodeNULLNode(10)Node(20)Node(30)NULL
new_node.prevN/ANULLNULLNULLN/A
new_node.nextN/ANULLNode(10)Node(20)N/A
Node(10).prevN/ANULLNode(20)Node(20)Node(20)
Node(20).prevN/AN/ANULLNode(30)Node(30)
Key Moments - 3 Insights
Why do we set new_node.prev to NULL when inserting at the beginning?
Because the new node becomes the first node, it has no previous node. See execution_table step 2 and 7 where new_node.prev is set to NULL.
Why do we update the old head's prev pointer to new_node?
To maintain the backward link in the doubly linked list. Without this, the old head would not point back to the new first node. See execution_table step 9 and 14.
What happens if the list is empty before insertion?
The head is NULL, so new_node.next is set to NULL and no prev pointer update is needed. See execution_table step 4 where head is NULL and step 5 where head is set to new_node.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 9, what pointer change happens?
AThe old head's prev pointer is set to new_node
BThe new_node.next is set to NULL
CThe head pointer is set to NULL
DThe new_node.prev is set to old head
💡 Hint
Check the 'Pointer Changes' column at step 9 in execution_table.
At which step does the head pointer first point to the new node with data 20?
AStep 5
BStep 15
CStep 10
DStep 2
💡 Hint
Look at the 'Pointer Changes' and 'Visual State' columns for when head changes to node with data 20.
If the list was not empty, what must be updated after setting new_node.next = head?
Anew_node.prev must be set to head
Bhead.prev must be set to new_node
Chead.next must be set to new_node
Dnew_node.next must be set to NULL
💡 Hint
Refer to execution_table steps 8 and 9 where new_node.next and head.prev are updated.
Concept Snapshot
Insert at Beginning of Doubly Linked List:
- Create new node with data
- Set new_node.prev = NULL
- Set new_node.next = current head
- If head exists, set head.prev = new_node
- Update head to new_node
- Maintains forward and backward links
- New node becomes first element
Full Transcript
To insert a node at the beginning of a doubly linked list, first create a new node and assign the data. Set the new node's prev pointer to NULL because it will be the first node. Set its next pointer to the current head of the list. If the list is not empty, update the old head's prev pointer to point back to the new node. Finally, update the head pointer to the new node. This ensures the list maintains correct forward and backward links. The execution table shows each step with pointer changes and the visual state of the list after each insertion.