📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each node holds the name of a song. Sometimes, you want to remove a song by its name from the playlist.
🎯 Goal: Build a doubly linked list of songs, then delete a song by its name, and finally print the updated playlist.
📋 What You'll Learn
Create a doubly linked list with exact song names in order
Add a variable to hold the song name to delete
Write a function to delete the first node with the given song name
Print the playlist after deletion showing all songs in order
💡 Why This Matters
🌍 Real World
Managing playlists, browser history, or undo-redo stacks often uses doubly linked lists where nodes need to be added or removed by value.
💼 Career
Understanding how to manipulate doubly linked lists is important for software engineers working with low-level data structures, memory management, and performance-critical applications.
Progress0 / 4 steps
1
Create the Doubly Linked List with Songs
Create a doubly linked list with nodes containing these exact song names in order: "SongA", "SongB", "SongC", "SongD". Use the struct Node with string data, Node* prev, and Node* next. Create a pointer head pointing to the first node.
DSA C++
Hint
Start by creating the first node with "SongA" and set head to it. Then create the next nodes, linking prev and next pointers properly.
2
Add the Song Name to Delete
Add a string variable called songToDelete and set it to "SongC".
DSA C++
Hint
Just create a string variable songToDelete and assign it the value "SongC".
3
Write the Delete by Value Logic
Write code to delete the first node in the doubly linked list whose data matches songToDelete. Use a pointer current to traverse from head. Update the prev and next pointers of neighboring nodes to remove the node. If the node to delete is head, update head accordingly. Free the deleted node's memory using delete.
DSA C++
Hint
Traverse the list with current. When you find the node with data == songToDelete, update pointers of neighbors and delete the node. Remember to handle if the node is the head.
4
Print the Updated Playlist
Print the updated doubly linked list starting from head. Use a pointer temp to traverse. Print each song's data followed by " -> ". After the last song, print "null".
DSA C++
Hint
Use a pointer temp starting at head. While temp is not nullptr, print temp->data followed by " -> ". Move temp to temp->next. After the loop, print "null".
Practice
(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. All nodes are deleted regardless of value.
B. The node with the matching value is removed and links are updated.
C. Only the first node is deleted without checking value.
D. The list is reversed instead of deleting.
Solution
Step 1: Locate the node with the given value
Traverse the list from head to tail to find the node whose data matches the value to delete.
Step 2: Update links to remove the node
Adjust the previous node's next pointer and the next node's previous pointer to bypass the node to be deleted.
Final Answer:
The node with the matching value is removed and links are updated. -> Option B
Quick Check:
Delete by value removes matching node [OK]
Hint: Find node, update previous and next pointers [OK]
Common Mistakes:
Not updating both previous and next pointers
Deleting node without checking value
Deleting all nodes instead of one
2. Which of the following is the correct syntax to delete a node by value in a doubly linked list in C++?
easy
A. void deleteNode(Node* &head, int val) { /* code */ }
B. void deleteNode(int val) { /* code */ }
C. Node deleteNode(int val) { /* code */ }
D. int deleteNode(Node* head, int val) { /* code */ }
Solution
Step 1: Understand parameter passing for head pointer
Head pointer must be passed by reference to allow modification if the first node is deleted.
Step 2: Check function return type
Deletion functions usually return void as they modify the list in place.
Final Answer:
void deleteNode(Node* &head, int val) { /* code */ } -> Option A
Quick Check:
Pass head by reference and return void [OK]
Hint: Pass head by reference to modify list [OK]
Common Mistakes:
Passing head by value causing no list update
Returning Node instead of void
Missing head pointer parameter
3. What is the output of the following code snippet after deleting value 20 from the doubly linked list?