A singly linked list is a linked list in which each node contains only one link field pointing the next node in the list.
Each node is divided in two parts.
1. data field.
2. pointer.
For Example: –
Node structure
The data field contains the data elements that have to be stored in the list. The pointer will point the next node in the list.
The operations done on a list are: –
1 Insertion
2 Deletion
Insertion
Insertion in the head node
To insert a node in the head node, just change the pointer field of the new node to point to the head node. Let the new node be ‘Temp’ and the head node be ‘Head’, then the insertion is
Temp ? data = X;
Head ? next = head
Insertion in the middle node
To insert in the middle node we need to change two pointers. Let the new node be ‘Temp’ and the present node is ‘Present’ and, the next node to the present node is ‘future’. The pointers used are ‘data’ for the data field, ‘next’ to the pointer field, the data to be inserted is ‘X ’then the insertion is
Temp? data = X
Present ? next = temp
Temp ? next = future
Insertion in the last node
To insert an element in the last position just change the pointer field of the present last node to point to the new node, then set the pointer field of the new node to NULL. Let the new node be ‘Temp’ and the present node is ‘Present’. The pointers used are ‘data’ for the data field, ‘next’ to the pointer field, the data to be inserted is ‘X ’then the insertion is
Present ? next =Temp
Temp ? next =null
Temp ? data = X
Deletion
Deletion in the head node
To delete a node in the head node, just point the head node as the second node. Let the head node be ‘Head’, and then the deletion is
Head ? next = head
Deletion in the middle node
To delete a node in the middle we need to change two pointers. Let the node to be deleted is ‘Temp’ and the node previous to the node to be deleted is ‘Present’ and, the next node to the present node is ‘future’. The pointers used are ‘data’ for the data field, ‘next’ to the pointer field, the data to be inserted is ‘X ’then the insertion is
Present ? next = future
Deletion in the last node
To delete an element in the last position just change the pointer field of the previous node to the last to null. Let the last node be ‘Temp’ and the previous node is ‘Present’. The pointers used are ‘data’ for the data field, ‘next’ to the pointer field, the data to be inserted is ‘X ’then the insertion is