Bird
0
0
DSA Cprogramming~10 mins

Insert at Specific Position in Doubly Linked List in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at Specific Position in Doubly Linked List
Create new node with data
Check if position is 1
Insert at head
Traverse to position-1 node
Adjust pointers: new node prev/next
Adjust neighbors' pointers
Done
Create a new node, then either insert at the head if position is 1, or traverse to the node before the target position and adjust pointers to insert the new node there.
Execution Sample
DSA C
void insertAtPosition(Node** head, int data, int pos) {
  Node* newNode = createNode(data);
  if (pos == 1) {
    newNode->next = *head;
    if (*head) (*head)->prev = newNode;
    *head = newNode;
    return;
  }
  Node* temp = *head;
  for (int i = 1; i < pos - 1 && temp != NULL; i++) temp = temp->next;
  if (temp == NULL) return; // position out of bounds
  newNode->next = temp->next;
  if (temp->next) temp->next->prev = newNode;
  temp->next = newNode;
  newNode->prev = temp;
}
This code inserts a new node with given data at the specified position in a doubly linked list.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Create new node with data=5N/AnewNode created10 <-> 20 <-> null
2Check if position == 1Head=Node(10)No pointer change10 <-> 20 <-> null
3Traverse to position-1 (pos=3)Start at head (10)temp points to 1010 <-> 20 <-> null
4Traverse iteration 1temp moves to 20temp updated10 <-> 20 <-> null
5Adjust newNode pointerstemp=20newNode->next = temp->next (null), newNode->prev = temp (20)10 <-> 20 <-> null
6Adjust neighbors' pointerstemp=20temp->next = newNode, newNode->next->prev = newNode (no next node)10 <-> 20 <-> 5 <-> null
7Insertion completeN/AAll pointers set10 <-> 20 <-> 5 <-> null
8ExitN/AInsertion done10 <-> 20 <-> 5 <-> null
💡 Insertion done at position 3, traversal ended after reaching position-1 node.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
headNode(10)Node(10)Node(10)Node(10)Node(10)
tempNode(10)Node(10)Node(20)Node(20)Node(20)
newNodeN/ANode(5)Node(5)Node(5)Node(5)
Key Moments - 3 Insights
Why do we stop traversal at position-1 instead of position?
We stop at position-1 (Step 3 and 4) because we need to insert the new node after this node. This allows us to adjust pointers correctly without losing the list structure.
What happens if the position is 1?
If position is 1 (Step 2), we insert the new node at the head by adjusting the head pointer and the previous head's prev pointer, as shown in the flow and code.
Why do we check if temp->next is NULL before adjusting its prev pointer?
Because if temp->next is NULL, it means we are inserting at the end. Trying to access temp->next->prev would cause an error. So we only update if temp->next exists (Step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of temp after Step 4?
ANode with data 20
BNode with data 5
CNode with data 10
DNULL
💡 Hint
Check the 'Current Node' column at Step 4 in the execution_table.
At which step is the new node's prev pointer set?
AStep 3
BStep 6
CStep 5
DStep 2
💡 Hint
Look at the 'Pointer Changes' column for Step 5 in the execution_table.
If position was 1, which step would handle the insertion?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Step 2 checks if position == 1 in the execution_table.
Concept Snapshot
Insert at Specific Position in Doubly Linked List:
- Create new node
- If position == 1, insert at head
- Else traverse to position-1 node
- Adjust new node's prev and next pointers
- Adjust neighbors' pointers
- Handle edge cases (empty list, end insertion)
Full Transcript
To insert a node at a specific position in a doubly linked list, first create the new node. If the position is 1, insert it at the head by adjusting the head pointer and the previous head's prev pointer. Otherwise, traverse the list to the node just before the desired position. Then, adjust the new node's next and prev pointers to link it between the nodes. Also, update the neighboring nodes' pointers to include the new node. This process ensures the list remains connected correctly. Edge cases like inserting at the end or empty list are handled by checking pointers before updating.