📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each song is a node with a title and links to the previous and next songs.Now, you want to add a new song at the beginning of the playlist.
🎯 Goal: Build a doubly linked list and insert a new node at the beginning. Then print the playlist from start to end.
📋 What You'll Learn
Create a struct called Node with char title[50], Node* prev, and Node* next
Create a doubly linked list with two nodes having titles "Song1" and "Song2"
Create a new node with title "NewSong" and insert it at the beginning of the list
Print the playlist titles from the beginning to the end separated by -> and ending with NULL
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and other apps to move forward and backward through items efficiently.
💼 Career
Understanding linked lists and pointer manipulation is essential for software developers working with low-level data structures and memory management.
Progress0 / 4 steps
1
Create the initial doubly linked list
Create a struct called Node with char title[50], Node* prev, and Node* next. Then create two nodes with titles "Song1" and "Song2" linked as a doubly linked list. Set head to point to the first node.
DSA C
Hint
Use typedef struct Node to define the node. Use malloc to create nodes. Use strcpy to set titles.
2
Create the new node to insert
Create a new node pointer called newNode using malloc. Set its title to "NewSong". Initialize its prev and next pointers to NULL.
DSA C
Hint
Use malloc to create newNode. Use strcpy to set the title. Set both pointers to NULL.
3
Insert the new node at the beginning
Insert newNode at the beginning of the doubly linked list. Set newNode->next to head, head->prev to newNode, and update head to point to newNode.
DSA C
Hint
Link newNode to the current head. Update head->prev to newNode. Then update head to newNode.
4
Print the doubly linked list
Use a Node* pointer called temp to traverse the list from head. Print each node's title followed by -> . After the last node, print NULL.
DSA C
Hint
Use a while loop to print each node's title followed by -> . End with NULL.
Practice
(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node after the tail node
B. To add a new node before the current head node
C. To delete the first node of the list
D. To reverse the entire list
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes connected both forward and backward, with a head and tail.
Step 2: Identify what inserting at the beginning means
Inserting at the beginning means adding a new node before the current head node, making it the new head.
Final Answer:
To add a new node before the current head node -> Option B
Quick Check:
Insert at beginning = add before head [OK]
Hint: Insert at beginning means new node becomes the head [OK]
Common Mistakes:
Confusing insertion at beginning with insertion at end
Thinking insertion deletes nodes
Mixing up head and tail positions
2. Which of the following is the correct way to update the previous pointer of the old head node when inserting a new node at the beginning in C?
easy
A. new_node->prev = old_head;
B. old_head->next = new_node;
C. new_node->next = old_head->prev;
D. old_head->prev = new_node;
Solution
Step 1: Identify pointer updates when inserting at beginning
The new node's next points to old head, and old head's prev points back to new node.
Step 2: Choose correct pointer update for old head
We must set old_head->prev = new_node to link backward to new node.
Final Answer:
old_head->prev = new_node; -> Option D
Quick Check:
Old head prev points to new node [OK]
Hint: Old head's prev points to new node after insertion [OK]
Common Mistakes:
Setting old_head->next instead of prev
Assigning new_node->prev incorrectly
Forgetting to update old head's prev pointer
3. What will be the output of the following code snippet after inserting a node with value 10 at the beginning of the doubly linked list?
A. head->next should be head->prev when updating old head
B. new_node->prev should not be NULL
C. head pointer should not be updated
D. malloc size is incorrect
Solution
Step 1: Check pointer updates for old head
When inserting at beginning, old head's prev pointer must point to new node, not next.
Step 2: Identify incorrect line
Code uses head->next = new_node; which wrongly sets old head's next pointer instead of prev.
Final Answer:
head->next should be head->prev when updating old head -> Option A
Quick Check:
Old head prev must point to new node [OK]
Hint: Old head prev, not next, points to new node [OK]
Common Mistakes:
Updating old head's next pointer instead of prev
Not updating head pointer
Incorrect malloc size usage
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25. You want to insert a new node with value 1 at the beginning. After insertion, what will be the correct sequence of node values when traversing from head to tail?
medium
A. 1 <-> 5 <-> 15 <-> 25
B. 5 <-> 15 <-> 25 <-> 1
C. 25 <-> 15 <-> 5 <-> 1
D. 1 <-> 15 <-> 5 <-> 25
Solution
Step 1: Understand insertion at beginning
New node with value 1 becomes new head, pointing next to old head (5).