Theory A singly linked list is a dynamic data structure consisting of nodes where each node contains a data part and a pointer to the next node. It allows dynamic memory allocation without requiring a fixed size.
🔹 Operations on a Singly Linked List
- Creation: Initialize an empty linked list.
- Insertion: Insert a new node at the beginning, end, or a specific position.
- Deletion: Delete a node from the beginning, end, or a specific position.
- Traversal: Display the elements of the linked list.
📌 Algorithm
1. Creation
Initialize the linked list with a head pointer set to NULL.
2. Insertion
- Create a new node.
- For beginning insertion: Point new node's
next to head and update head.
- For end insertion: Traverse to last node and link new node.
- For specific position insertion: Traverse and adjust pointers.
3. Deletion
- For beginning deletion: Update
head and free the removed node.
- For end deletion: Traverse to second last node, update
next to NULL, and free the last node.
- For specific position deletion: Adjust pointers to skip the deleted node.
4. Traversal
Start from head, traverse each node, and print its data.