Bird
0
0
DSA Cprogramming~10 mins

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

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

void insertEnd(struct Node** head, int val) {
  // Insert val at end
}
Insert a new node with given value at the end of a doubly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10Emptynew_node created with data=10, prev=∅, next=∅∅ (empty list)
2Check if list is emptyEmptyhead == ∅ (true)∅ (empty list)
3Set head = new_node1 nodehead → new_node┌────────┐ │ data:10│ │ prev:∅ │ │ next:∅ │ └────────┘ head
4Create new node with data=201 nodenew_node created with data=20, prev=∅, next=∅┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ prev:∅ │ │ prev:∅ │ │ next:∅ │ │ next:∅ │ └────────┘ └────────┘ head
5Check if list is empty1 nodehead != ∅ (false)┌────────┐ │ data:10│ │ prev:∅ │ │ next:∅ │ └────────┘ head
6Traverse to last node1 nodecurrent = head (data=10), current.next == ∅ (stop)┌────────┐ │ data:10│ │ prev:∅ │ │ next:∅ │ └────────┘ head, current
7Set last.next = new_node2 nodescurrent.next → new_node (data=20)┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ prev:∅ │ │ prev:∅ │ │ next:──┼──→ │ next:∅ │ └────────┘ └────────┘ head current.next
8Set new_node.prev = last2 nodesnew_node.prev → current (data=10)┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ prev:∅ │ │ prev:──┼──→ head │ next:──┼──→ │ next:∅ │ └────────┘ └────────┘ head new_node
9Done2 nodesList updated with new tail┌────────┐ ┌────────┐ │ data:10│ │ data:20│ │ prev:∅ │←───┤ prev:──┼──→ ∅ │ next:──┼──→ │ next:∅ │ └────────┘ └────────┘ head tail
💡 Insertion complete; new node linked at end with correct prev and next pointers.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6After Step 7After Step 8Final
head→ node(10)→ node(10)→ node(10)→ node(10)→ node(10)→ node(10)
new_nodenode(10)node(10)node(20)node(20)node(20)node(20)node(20)
currentnode(10)node(10)node(10)node(10)
Key Moments - 3 Insights
Why do we check if the list is empty before inserting?
Because if the list is empty (head == ∅), we must set head to the new node directly (see execution_table step 2 and 3). Otherwise, traversal to the last node is needed.
Why do we set new_node.prev to the last node?
In a doubly linked list, each node points back to its previous node. Setting new_node.prev to last node maintains this backward link (see execution_table step 8).
How do we know where to insert the new node?
We traverse from head until current.next is ∅, meaning current is the last node. Then we link new_node after current (see execution_table step 6 and 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of head after step 3?
Ahead is still empty (∅)
Bhead points to node with data 10
Chead points to node with data 20
Dhead points to new_node with data 20
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3 in execution_table.
At which step does the traversal to the last node stop?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table to find when current.next == ∅.
If the list was empty, which step would be skipped?
AStep 6 (traverse to last node)
BStep 3 (set head = new_node)
CStep 2 (check if list is empty)
DStep 8 (set new_node.prev = last)
💡 Hint
Refer to the concept_flow and execution_table steps for empty list insertion.
Concept Snapshot
Insert at End of Doubly Linked List:
- Create new node with data
- If list empty, set head = new node
- Else traverse to last node
- Set last.next = new node
- Set new_node.prev = last node
- New node's next = ∅ (null)
- List size increases by 1
Full Transcript
To insert at the end of a doubly linked list, first create a new node with the given data. Check if the list is empty by seeing if head is null. If empty, set head to the new node. If not, start from head and move through nodes until reaching the last node (where next is null). Then link the new node after the last node by setting last.next to new node and new_node.prev to last node. The new node's next pointer is set to null. This maintains the doubly linked structure with forward and backward links. The list now has one more node at the end.