Bird
0
0
DSA Cprogramming~30 mins

Delete Node by Value in DSA C - Build from Scratch

Choose your learning style9 modes available
Delete Node by Value in a Linked List
📖 Scenario: You are managing a simple contact list stored as a linked list. Each node contains a contact's id and a pointer to the next contact. You want to remove a contact by their id.
🎯 Goal: Build a C program that creates a linked list of contacts, sets a target id to delete, deletes the node with that id, and prints the updated list.
📋 What You'll Learn
Create a linked list with exactly 4 nodes having id values 10, 20, 30, and 40 in that order
Create an integer variable target_id and set it to 30
Write a function deleteNodeByValue that deletes the node with id equal to target_id
Print the linked list after deletion in the format: 10 -> 20 -> 40 -> NULL
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like contact lists, music playlists, and undo history where items can be added or removed dynamically.
💼 Career
Understanding linked list operations like deletion is fundamental for software developers working with low-level data structures, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Create the linked list with 4 nodes
Write code to create a linked list with 4 nodes having id values 10, 20, 30, and 40 in that order. Use a struct called Node with an int id and a Node* called next. Create a pointer head pointing to the first node.
DSA C
Hint

Use malloc to create each node and set the id and next pointers accordingly.

2
Set the target id to delete
Create an integer variable called target_id and set it to 30.
DSA C
Hint

Just declare int target_id = 30; inside main.

3
Write the function to delete node by value
Write a function called deleteNodeByValue that takes a pointer to the pointer head and an integer target_id. It deletes the first node with id equal to target_id from the linked list. Use variables temp and prev inside the function. Call this function from main with &head and target_id.
DSA C
Hint

Check if the head node itself has the target id. If yes, update head. Otherwise, traverse the list to find the node and remove it by changing the previous node's next pointer.

4
Print the linked list after deletion
Write a printList function that takes head and prints the id values of nodes separated by -> and ending with NULL. Call printList(head) in main after deleting the node.
DSA C
Hint

Traverse the list and print each node's id followed by ->. End with NULL and a newline.