Bird
0
0
DSA Cprogramming~30 mins

Delete Node at Beginning in DSA C - Build from Scratch

Choose your learning style9 modes available
Delete Node at Beginning in a Linked List
📖 Scenario: You are managing a simple list of tasks using a linked list. Sometimes, you need to remove the first task when it is completed.
🎯 Goal: Build a program that creates a linked list with three tasks, deletes the first task (node) from the list, and then prints the remaining tasks.
📋 What You'll Learn
Create a linked list with exactly three nodes containing the values 10, 20, and 30 in that order.
Write a function called deleteAtBeginning that removes the first node from the linked list.
Print the linked list after deleting the first node in the format: 10 -> 20 -> 30 -> NULL before deletion and 20 -> 30 -> NULL after deletion.
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like task scheduling, undo functionality in editors, and managing playlists where items are added or removed dynamically.
💼 Career
Understanding linked list operations like deletion is fundamental for software developers working with dynamic data structures, memory management, and system programming.
Progress0 / 4 steps
1
Create a linked list with three nodes
Create a linked list with three nodes containing the values 10, 20, and 30 in that order. Use a struct Node with an int data and a struct Node* next. Create a pointer called head that points to the first node.
DSA C
Hint

Remember to allocate memory for each node using malloc and link them by setting the next pointer.

2
Write a function to delete the first node
Write a function called deleteAtBeginning that takes a pointer to the pointer head of the linked list and deletes the first node. It should update head to point to the second node and free the memory of the removed node.
DSA C
Hint

Use a temporary pointer to hold the first node, update *head to the second node, then free the first node.

3
Call the delete function and prepare to print
In the main function, call deleteAtBeginning with the address of head to delete the first node. Then, create a pointer called current and set it to head to prepare for printing the list.
DSA C
Hint

Call the function with &head and then set current to head for traversal.

4
Print the linked list after deletion
Use a while loop with the pointer current to print the linked list nodes' data separated by ->. After the last node, print NULL. The output should be: 20 -> 30 -> NULL
DSA C
Hint

Use a while loop to print each node's data followed by ->, and print NULL at the end.