Bird
0
0
DSA Cprogramming~10 mins

Insert at Beginning Head Insert in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at Beginning Head Insert
Create new node
Set new_node->next = head
Update head = new_node
New node is now the first node in list
This flow shows how a new node is created and placed at the start of the linked list by adjusting pointers.
Execution Sample
DSA C
struct Node {
    int data;
    struct Node* next;
};

void insertAtBeginning(struct Node** head, int val) {
    struct Node* new_node = malloc(sizeof(struct Node));
    new_node->data = val;
    new_node->next = *head;
    *head = new_node;
}
This code creates a new node and inserts it at the beginning of the linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with empty listhead = ∅List is empty: head → ∅
2Create new node with data=10Node(10)new_node created, next unassignednew_node: [data:10 | next: ?]
3Set new_node->next = head (∅)Node(10)new_node->next → ∅new_node: [data:10 | next: ∅]
4Update head = new_nodeNode(10)head → new_nodehead → [data:10 | next: ∅]
5Insert new node with data=20 at beginningNode(20) → Node(10)new_node created, new_node->next = head, head updatedhead → [data:20 | next: ] → [data:10 | next: ∅]
6Insert new node with data=30 at beginningNode(30) → Node(20) → Node(10)new_node created, new_node->next = head, head updatedhead → [data:30 | next: ] → [data:20 | next: ] → [data:10 | next: ∅]
7End of insertionsNode(30) → Node(20) → Node(10)No pointer changesFinal list: head → 30 → 20 → 10 → ∅
💡 All insertions done, head points to newest node, list built from front
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6Final
head[data:10 | next: ∅][data:20 | next: ][data:30 | next: ][data:30 | next: ]
new_node[data:10 | next: ?][data:10 | next: ∅][data:20 | next: ][data:30 | next: ]∅ (no longer needed)
Key Moments - 3 Insights
Why do we set new_node->next = head before updating head?
Because we want the new node to point to the current first node before head changes. See execution_table step 3 and 4 where new_node->next is set to old head, then head is updated to new_node.
What happens if the list is empty when inserting?
The new node's next points to null (∅), and head points to the new node. This is shown in steps 1 to 4 where head starts as ∅ and after insertion points to the new node.
How does the list grow after multiple insertions at beginning?
Each new node points to the previous head, making it the new first node. The list visually grows from the front as shown in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does head point to?
AThe old head before insertion
BNull (∅)
CThe new node with data 10
DThe new node with data 20
💡 Hint
Check the Pointer Changes and Visual State columns at step 4 in execution_table
At which step does the list first contain two nodes?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the Nodes in List column to see when two nodes appear
If we skipped setting new_node->next = head, what would happen?
AThe list would be empty
BThe new node would not link to the rest of the list
CThe head pointer would not change
DThe new node would point to itself
💡 Hint
Refer to the key moment about why new_node->next is set before updating head
Concept Snapshot
Insert at Beginning (Head Insert):
- Create new node
- Set new_node->next = head
- Update head = new_node
- New node becomes first in list
- List grows by adding nodes at front
Full Transcript
This concept shows how to insert a new node at the beginning of a singly linked list. First, a new node is created with the given data. Then, the new node's next pointer is set to the current head of the list. Finally, the head pointer is updated to point to the new node. This makes the new node the first node in the list. Repeating this process adds nodes to the front, growing the list from the beginning.