📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each node contains a song number. You want to remove the last song from the playlist.
🎯 Goal: Build a doubly linked list with 3 nodes, then delete the last node, and finally print the updated list.
📋 What You'll Learn
Create a doubly linked list with exactly 3 nodes containing data 10, 20, and 30
Create a pointer called head pointing to the first node
Create a pointer called tail pointing to the last node
Write a function called deleteFromEnd that deletes the last node from the list
Print the list from head to tail after deletion
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and undo-redo features where you need to move forward and backward easily.
💼 Career
Understanding linked list operations is fundamental for software developers working with data structures, memory management, and system programming.
Progress0 / 4 steps
1
Create the doubly linked list with 3 nodes
Create a struct called Node with int data, Node *prev, and Node *next. Then create 3 nodes with data 10, 20, and 30. Link them so that head points to the first node and tail points to the last node.
DSA C
Hint
Remember to link each node's next and prev pointers correctly.
2
Create the deleteFromEnd function
Create a function called deleteFromEnd that takes Node **head and Node **tail as parameters. It should delete the last node from the doubly linked list and update tail accordingly.
DSA C
Hint
Check if the list is empty or has only one node before deleting.
3
Traverse and print the list from head to tail
Create a Node * pointer called current and set it to head. Use a while loop to traverse the list and print each node's data followed by ->. After the loop, print NULL.
DSA C
Hint
Use a while loop to move through the list until current is NULL.
4
Print the updated list after deletion
Run the program and observe the printed output. It should show the list after deleting the last node. The output should be: 10 -> 20 -> NULL
DSA C
Hint
Run the program to see the updated list after deleting the last node.
Practice
(1/5)
1. What happens to the tail pointer when you delete the last node from a doubly linked list?
easy
A. It becomes NULL immediately without checking.
B. It remains unchanged.
C. It points to the head node.
D. It moves to the previous node of the deleted node.
Solution
Step 1: Understand the tail pointer role
The tail pointer always points to the last node in the doubly linked list.
Step 2: Update tail after deletion
When the last node is deleted, the tail pointer must move to the previous node to keep pointing to the new last node.
Final Answer:
It moves to the previous node of the deleted node. -> Option D
Quick Check:
Tail points to previous node after deletion [OK]
Hint: Tail always points to last node; update to previous on delete [OK]
Common Mistakes:
Setting tail to NULL without checking if list is empty
Not updating tail pointer after deletion
Pointing tail to head incorrectly
2. Which of the following is the correct syntax to delete the last node in a doubly linked list in C?
easy
A. tail = tail->prev; free(tail->next); tail->next = NULL;
B. free(tail); tail = tail->prev; tail->next = NULL;
C. tail->prev = tail; free(tail); tail = NULL;
D. tail->next = NULL; free(tail); tail = tail->prev;
Solution
Step 1: Free the last node correctly
We first move tail to previous node, then free the old last node which is tail->next.
Step 2: Update tail's next pointer
After freeing, set tail->next to NULL to mark new end of list.
Final Answer:
tail = tail->prev; free(tail->next); tail->next = NULL; -> Option A
Quick Check:
Move tail first, free old node, update next pointer [OK]
Hint: Move tail first, then free old node, update next pointer [OK]