Singly Linked List
Singly Linked List
A linked list is a linear data structure used to store elements of the same data
type but not in contiguous memory locations. It is a collection of nodes where
each node contains a data field and a next pointer indicating the address of
the next node. So only the current node in the list knows where the next
element of the list is stored. In this article, we will learn how to implement a
singly linked list in C.
where,
● next: is a pointer that will store the address of the next node in the
sequence.
The following diagram represents the structure of a singly linked list:
Following is the algorithm to insert a node at the start of the singly linked
list:
● If the linked list is empty set the new node as the Head and return.
● Connect the next pointer of this new node to the Head of the linked
list.
● Update the Head pointer and make it points to the new node
linked list.
● Iterate over the linked list to find the node before the insertion point
(position – 1).
range. Return.
● Point the next pointer of the new node to the node present just after
● Point the next pointer of the temporary node to the new node and
return.
Following is the algorithm to insert a node at the end of the singly linked list:
● If the list is empty, update the Head pointer to be this new node and
then return.
● Otherwise traverse till the last node of the singly linked list.
Following is the algorithm to delete the first node of the singly linked list:
1. Ensure that the Head of the linked list is not NULL; if it is, the list is
empty, so return.
list.
3. Update the current head of the singly linked list to the next node.
linked list.
● Iterate the linked list to find the node before the deletion point
(position – 1).
● Update the next pointer of the temporary pointer to the next pointer
Following is the algorithm to delete the last node of the linked list:
● Ensure that the Head of the linked list is not NULL; if it is, the list is
empty, so return.
● If the singly linked list has only one node, delete the head node and
● Traverse till the second last node of the singly linked list.
● Store the next node of the second last node in a temporary pointer.
return back.
○ Print temp->data.
// Function to insert a new element at the end of the singly linked list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
// Driver Code
int main() {
struct Node* head = NULL;
insertAtFirst(&head, 10);
printf("Linked list after inserting the node:10 at the beginning \n");
print(head);
Output
Linked list after inserting the node:10 at the beginning
10 -> NULL
Linked list after inserting the node:20 at the end
10 -> 20 -> NULL
Linked list after inserting the node:5 at the end
10 -> 20 -> 5 -> NULL
Linked list after inserting the node:30 at the end
10 -> 20 -> 5 -> 30 -> NULL
Linked list after inserting the node:15 at position 2
10 -> 20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the first node:
20 -> 15 -> 5 -> 30 -> NULL
Linked list after deleting the last node:
20 -> 15 -> 5 -> NULL
Linked list after deleting the node at position 1:
20 -> 5 -> NULL