Bird
0
0
DSA Cprogramming~30 mins

Delete Node at End in DSA C - Build from Scratch

Choose your learning style9 modes available
Delete Node at End in a Singly Linked List
📖 Scenario: Imagine you have a chain of boxes connected one after another. Each box holds a number and points to the next box. This is like a singly linked list. Sometimes, you want to remove the last box from the chain.
🎯 Goal: You will create a singly linked list with 3 nodes, then write code to remove the last node from the list, and finally print the list to see the result.
📋 What You'll Learn
Create a singly linked list with exactly 3 nodes containing values 10, 20, and 30 in that order
Create a pointer variable called head pointing to the first node
Write a function called deleteAtEnd that removes the last node from the list
Print the list after deletion showing the nodes connected with arrows like: 10 -> 20 -> NULL
💡 Why This Matters
🌍 Real World
Linked lists are used in many software systems to manage collections of items where size changes often, like music playlists or undo history.
💼 Career
Understanding how to manipulate linked lists is fundamental for software developers working with data structures, memory management, and system programming.
Progress0 / 4 steps
1
Create the singly linked list with 3 nodes
Create a struct called Node with an int data and a struct Node* next. Then create 3 nodes with values 10, 20, and 30. Link them so that head points to the first node.
DSA C
Hint

Remember to use malloc to create nodes and link them by setting the next pointer.

2
Create the deleteAtEnd function
Write a function called deleteAtEnd that takes a pointer to Node* and removes the last node from the list. Handle the case when the list has only one node.
DSA C
Hint

Use two pointers to find the second last node. Free the last node and set second last node's next to NULL.

3
Print the linked list
Write a function called printList that takes Node* head and prints the list nodes in the format: 10 -> 20 -> NULL. Then call printList(head) in main after deleting the last node.
DSA C
Hint

Use a loop to print each node's data followed by '->'. After the loop, print 'NULL'.

4
Print the final linked list after deletion
Run the program and observe the printed output after deleting the last node. The output should be: 10 -> 20 -> NULL
DSA C
Hint

Make sure you call printList(head) after deleting the last node to see the updated list.