📖 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. You want to remove the first song from the playlist.
🎯 Goal: Build a program that creates a doubly linked list with three songs, then deletes the first song from the list, and finally prints the updated playlist.
📋 What You'll Learn
Create a doubly linked list with exactly three nodes containing the song titles: "Song1", "Song2", and "Song3"
Create a pointer variable called head that points to the first node
Write a function called deleteFromBeginning that deletes the first node of the doubly linked list
Print the updated list from the head to the end after deletion
💡 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 list operations is fundamental for software developers working with dynamic data structures and memory management.
Progress0 / 4 steps
1
Create the doubly linked list with three songs
Create a struct called Node with members char title[20], struct Node *prev, and struct Node *next. Then create three nodes with titles "Song1", "Song2", and "Song3". Link them to form a doubly linked list. Create a pointer called head that points to the first node.
DSA C
Hint
Use malloc to create nodes and strcpy to set titles. Link nodes by setting prev and next pointers.
2
Create a function to delete the first node
Create a function called deleteFromBeginning that takes a pointer to Node * called head and deletes the first node of the doubly linked list. Update head to point to the new first node. Handle the case when the list is empty or has only one node.
DSA C
Hint
Use a double pointer Node **head to modify the head pointer inside the function. Free the old first node after updating the head.
3
Call the delete function to remove the first song
In the main function, call the deleteFromBeginning function with the address of head to delete the first node from the doubly linked list.
DSA C
Hint
Call the function with &head to pass the address of the head pointer.
4
Print the updated doubly linked list
Write a while loop in main to print the titles of the songs in the updated doubly linked list starting from head. Print each title followed by " -> ". After the last node, print "NULL".
DSA C
Hint
Use a while loop to traverse from head to the end, printing each title followed by " -> ". After the loop, print "NULL".
Practice
(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It remains unchanged
B. It becomes NULL immediately
C. It moves to the second node in the list
D. It points to the last node
Solution
Step 1: Understand the head pointer role
The head pointer points to the first node in the doubly linked list.
Step 2: Effect of deleting the first node
When the first node is deleted, the head pointer must move to the next node, which becomes the new first node.
Final Answer:
It moves to the second node in the list -> Option C
Quick Check:
Head points to new first node after deletion [OK]
Hint: Head always moves to next node after deleting first [OK]
Common Mistakes:
Assuming head becomes NULL even if list has more nodes
Not updating head pointer after deletion
Pointing head to last node mistakenly
2. Which of the following is the correct syntax to delete the first node in a doubly linked list in C?
easy
A. temp = head; head = head->next; free(temp); head->prev = NULL;
B. free(head); head = head->next; head->prev = NULL;
C. head = head->next; free(head->prev); head->prev = NULL;
D. head->prev = NULL; free(head); head = head->next;
Solution
Step 1: Store current head in temp
We save the current first node in a temporary pointer to free it later.
Step 2: Move head to next node and free old head
Update head to point to the second node, free the old first node, and set new head's prev to NULL.
Final Answer:
temp = head; head = head->next; free(temp); head->prev = NULL; -> Option A