Bird
0
0
DSA Cprogramming~10 mins

Insert at End Tail Insert in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at End Tail Insert
Create new node with data
Is list empty?
Set head = new
Traverse until current.next == NULL
Set current.next = new node
Done
Create a new node, check if list is empty, if yes set head to new node; else traverse to last node and link new node at end.
Execution Sample
DSA C
struct Node {
  int data;
  struct Node* next;
};

void insertAtEnd(struct Node** head, int val) {
  // Insert val at list end
  struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  new_node->data = val;
  new_node->next = NULL;

  if (*head == NULL) {
    *head = new_node;
    return;
  }

  struct Node* current = *head;
  while (current->next != NULL) {
    current = current->next;
  }
  current->next = new_node;
}
Insert a new node with given value at the end of a singly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10new_node created with data=10, next=∅
2Check if head == NULLhead == NULL (list empty)
3Set head = new_node10head → new_node┌────────┐ │ data:10│ │ next:∅ │ └────────┘ head
4Create new node with data=2010new_node created with data=20, next=∅┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:∅ │ │ next:∅ │ └────────┘ └────────┘ head
5Check if head == NULL10head != NULL (list not empty)┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:∅ │ │ next:∅ │ └────────┘ └────────┘ head
6Traverse to last node (current = head)10current → node with data=10┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:∅ │ │ next:∅ │ └────────┘ └────────┘ head new_node
7Check current.next == NULL10current.next == NULL (true)┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ next:∅ │ │ next:∅ │ └────────┘ └────────┘ head new_node
8Set current.next = new_node10 -> 20node 10.next → new_node┌────────┐ ┌────────┐ │ data:10│───→│ data:20│ │ next:──┼ │ next:∅ │ └────────┘ └────────┘ head ✓ tail
9Done10 -> 20No pointer changes┌────────┐ ┌────────┐ │ data:10│───→│ data:20│ │ next:──┼ │ next:∅ │ └────────┘ └────────┘ head tail
💡 Traversal ends when current.next is NULL; new node linked as last node.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6After Step 8Final
headNULLNULLpoints to node 10points to node 10points to node 10points to node 10points to node 10
new_nodeNULLnode 10node 10node 20node 20node 20node 20
currentNULLNULLNULLNULLnode 10node 10node 10
Key Moments - 3 Insights
Why do we check if head == NULL before inserting?
Because if the list is empty (head == NULL), we must set head to the new node directly (see step 2 and 3 in execution_table). Otherwise, we traverse the list.
Why do we stop traversal when current.next == NULL, not current == NULL?
Because current.next == NULL means current is the last node. We want to link the new node after current. If we checked current == NULL, we would go past the last node (see step 7).
What pointer changes when we insert the new node at the end?
The last node's next pointer changes from NULL to point to the new node (see step 8). The new node's next remains NULL.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does head point to?
AThe new node with data 10
BNULL
CThe new node with data 20
DThe last node in the list
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3.
At which step does the traversal stop before inserting the new node?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for the step where current.next == NULL is true in the execution_table.
If the list was empty, which step sets the head pointer?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
See where head is assigned to new_node in the execution_table.
Concept Snapshot
Insert at End (Tail Insert) in Linked List:
- Create new node with data and next = NULL
- If list empty (head == NULL), set head = new node
- Else traverse list until last node (current.next == NULL)
- Set last node's next = new node
- New node becomes new tail with next = NULL
Full Transcript
This concept shows how to insert a new node at the end of a singly linked list. First, a new node is created with the given data and its next pointer set to NULL. Then, we check if the list is empty by seeing if head is NULL. If empty, we set head to the new node. If not empty, we start from head and traverse nodes until we find the last node whose next pointer is NULL. We then set that last node's next pointer to the new node, effectively adding it at the end. The new node's next remains NULL, marking it as the new tail. This process ensures the list grows by adding nodes at the end.