📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each song has a unique ID. You want to insert a new song at a specific position in the playlist.
🎯 Goal: Build a program that creates a doubly linked list with initial songs, sets the position to insert a new song, inserts the new song at that position, and then prints the updated playlist.
📋 What You'll Learn
Create a doubly linked list with exactly three nodes having song IDs 101, 102, and 103 in that order.
Create an integer variable called position and set it to 2.
Create a new node with song ID 150 and insert it at the position in the doubly linked list.
Print the updated doubly linked list from head to tail showing song IDs separated by arrows.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (history navigation), and many applications where you need to move forward and backward efficiently.
💼 Career
Understanding how to insert nodes in doubly linked lists is important for software engineers working on low-level data structures, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Create the initial doubly linked list
Create a doubly linked list with three nodes having song IDs 101, 102, and 103 in that order. Use a struct called Node with int data, Node* prev, and Node* next. Create a pointer called head pointing to the first node.
DSA C
Hint
Remember to allocate memory for each node using malloc and link the prev and next pointers correctly.
2
Set the position to insert the new node
Create an integer variable called position and set it to 2. This means the new song will be inserted as the second node in the list.
DSA C
Hint
Just declare an integer variable named position and assign it the value 2.
3
Insert the new node at the specified position
Create a new node with song ID 150 and insert it at the position in the doubly linked list. Update the prev and next pointers of the surrounding nodes accordingly.
DSA C
Hint
Traverse to the node before the position, then adjust pointers to insert the new node.
4
Print the updated doubly linked list
Print the updated doubly linked list from head to the end. Display the song IDs separated by arrows like 101 <-> 150 <-> 102 <-> 103.
DSA C
Hint
Use a while loop to traverse from head to the end, printing each node's data. Print arrows between nodes but not after the last node.
Practice
(1/5)
1. What happens when you insert a new node at position 1 in a doubly linked list?
easy
A. The new node becomes the new head of the list.
B. The new node is added at the end of the list.
C. The new node replaces the last node.
D. The list remains unchanged.
Solution
Step 1: Understand position 1 insertion
Inserting at position 1 means adding the node at the start of the list.
Step 2: Effect on head pointer
The new node becomes the head, and its next points to the old head.
Final Answer:
The new node becomes the new head of the list. -> Option A
Quick Check:
Position 1 insertion updates head [OK]
Hint: Position 1 means new head insertion [OK]
Common Mistakes:
Thinking position 1 means end insertion
Not updating the head pointer
Ignoring previous pointer updates
2. Which of the following is the correct way to update pointers when inserting a node at position 3 in a doubly linked list?
Hint: Update newNode next/prev before changing temp pointers [OK]
Common Mistakes:
Swapping next and prev pointers
Not updating temp->next->prev
Forgetting to update temp->next
3. What will be the list after inserting a node with value 10 at position 2 in the doubly linked list: 5 <-> 15 <-> 20?
medium
A. 5 <-> 15 <-> 10 <-> 20
B. 10 <-> 5 <-> 15 <-> 20
C. 5 <-> 15 <-> 20 <-> 10
D. 5 <-> 10 <-> 15 <-> 20
Solution
Step 1: Locate position 2
Position 2 means new node goes between nodes 1 (5) and 2 (15).
Step 2: Insert new node with value 10
New node's prev points to 5, next points to 15; update 5's next and 15's prev accordingly.
Final Answer:
5 <-> 10 <-> 15 <-> 20 -> Option D
Quick Check:
Inserted node at position 2 between 5 and 15 [OK]
Hint: Position 2 means between first and second nodes [OK]
Common Mistakes:
Inserting at wrong position
Not updating both next and prev pointers
Appending instead of inserting
4. Given the code snippet below, what is the error when inserting at position 4 in a list of 3 nodes?
void insertAtPosition(Node** head, int data, int pos) {
Node* newNode = createNode(data);
Node* temp = *head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
newNode->prev = temp;
temp->next->prev = newNode;
temp->next = newNode;
}
medium
A. newNode is not allocated properly.
B. temp->next is NULL, so temp->next->prev causes a runtime error.
C. Loop runs one time too many causing segmentation fault.
D. prev pointer of newNode is not set.
Solution
Step 1: Analyze loop for position 4 in 3-node list
Loop moves temp to node at position 3; temp->next is NULL (end of list).
Step 2: Check pointer updates
Accessing temp->next->prev causes runtime error because temp->next is NULL.
Final Answer:
temp->next is NULL, so temp->next->prev causes a runtime error. -> Option B
Quick Check:
Accessing NULL pointer causes runtime error [OK]
Hint: Check for NULL before accessing next->prev [OK]
Common Mistakes:
Not checking if temp->next is NULL
Assuming list is longer than it is
Ignoring edge case at list end
5. You have a doubly linked list: 1 <-> 3 <-> 5 <-> 7. You want to insert a node with value 4 at position 3. After insertion, what will be the correct list and how do you handle the pointers to maintain list integrity?
hard
A. 1 <-> 3 <-> 4 <-> 5 <-> 7; Set newNode->prev = node at pos 2, newNode->next = node at pos 3, update adjacent nodes' pointers.
B. 1 <-> 3 <-> 5 <-> 4 <-> 7; Insert newNode after node at pos 3 without updating prev pointers.
C. 1 <-> 4 <-> 3 <-> 5 <-> 7; Insert newNode at head and shift others.
D. 1 <-> 3 <-> 5 <-> 7 <-> 4; Append newNode at end regardless of position.
Solution
Step 1: Identify insertion position and nodes
Position 3 means new node goes between nodes with values 3 (pos 2) and 5 (pos 3).
Step 2: Update pointers to insert new node
Set newNode->prev to node with value 3, newNode->next to node with value 5; update node 3's next and node 5's prev to newNode.
Step 3: Verify list integrity
After insertion, list is 1 <-> 3 <-> 4 <-> 5 <-> 7 with all pointers correctly linked.
Final Answer:
1 <-> 3 <-> 4 <-> 5 <-> 7; Set newNode->prev = node at pos 2, newNode->next = node at pos 3, update adjacent nodes' pointers. -> Option A
Quick Check:
Proper pointer updates maintain doubly linked list [OK]
Hint: Insert between nodes, update both next and prev pointers [OK]
Common Mistakes:
Inserting at wrong position
Not updating both adjacent nodes' pointers
Breaking list links causing traversal errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.