A singly linked list is a linear data structure consisting of nodes that are connected using pointers rather than being stored in contiguous memory locations.
- Each node contains a data field and a pointer to the next node.
- The head pointer stores the address of the first node, while the last node points to NULL.
- Nodes can be dynamically allocated and linked at runtime.

Implementation of Singly Linked List
A singly linked list is a linear data structure in which each node stores data and the address of the next node. The last node points to NULL, indicating the end of the list. For example, a linked list containing 10, 20, and 30 can be represented as:
10 -> 20 -> 30 -> NULL
In C, each node can be represented using a struct:
struct Node {
int data;
struct Node* next;
};
- data: Stores the value of the node.
- next: Stores the address of the next node.
- The head pointer stores the address of the first node.
Singly Linked List Basic Operations
Following are some of the basic operations which are required to manipulate the nodes of a singly linked list:
Note: Here N represents the number of nodes in the linked list.
Insertion Operations in a Singly Linked List
1. Insert at Head
The insertAtFirst() function inserts a new node at the beginning of the linked list.
Algorithm:
- Create a new node.
- Store the given data in the new node.
- Set the next pointer of the new node to the current head.
- Update head to point to the new node.
2. Insert at a Given Position
The insertAtPosition() function inserts a new node at a specified position in the linked list.
Algorithm:
- If the position is 0, call insertAtFirst().
- Traverse the list until the node at position - 1 is reached.
- If the required position does not exist, display an error message and return.
- Create a new node.
- Set the new node's next pointer to the next node.
- Update the previous node's next pointer to the new node.
3. Insert at End
The insertAtEnd() function inserts a new node at the end of the linked list.
Algorithm:
- Create a new node.
- If the list is empty, make the new node the head and return.
- Traverse the list until the last node is reached.
- Set the next pointer of the last node to the new node.
Deletion Operations in a Singly Linked List
1. Delete from Head
The deleteFromFirst() function removes the first node from the linked list.
Algorithm:
- Check whether the list is empty.
- Store the current head in a temporary pointer.
- Update head to point to the next node.
- Free the memory occupied by the previous head node.
2. Delete from a Given PositionÂ
The deleteAtPosition() function removes a node from a specified position.
Algorithm:
- Check whether the list is empty.
- If the position is 0, call deleteFromFirst().
- Traverse the list until the node at position - 1 is reached.
- If the position is out of range, display an error message and return.
- Store the node to be deleted.
- Update the previous node's next pointer to skip the node being deleted.
- Free the memory occupied by the deleted node.
3. Delete from End
The deleteFromEnd() function removes the last node from the linked list.
Algorithm:
- Check whether the list is empty.
- If the list contains only one node, free it and set head to NULL.
- Otherwise, traverse the list until the second-last node is reached.
- Store the last node temporarily.
- Set the second-last node's next pointer to NULL.
- Free the last node.
Print Operation
The print() function traverses the linked list from the head node and prints the data stored in each node.
Algorithm:
- Set a temporary pointer to head.
- Traverse the linked list while the temporary pointer is not NULL.
- Print the data stored in the current node.
- Move the temporary pointer to the next node.
- Stop when the temporary pointer becomes NULL.
Example: Program to Implement Singly Linked List
#include <stdio.h>
#include <stdlib.h>
// Define the Node structure
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a new element at the beginning of the singly linked list
void insertAtFirst(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
// 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;
}
// Function to insert a new element at a specific position in the singly linked list
void insertAtPosition(struct Node** head, int data, int position) {
struct Node* newNode = createNode(data);
if (position == 0) {
insertAtFirst(head,data);
return;
}
struct Node* temp = *head;
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
// Function to delete the first node of the singly linked list
void deleteFromFirst(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = temp->next;
free(temp);
}
// Function to delete the last node of the singly linked list
void deleteFromEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
}
// Function to delete a node at a specific position in the singly linked list
void deleteAtPosition(struct Node** head, int position) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (position == 0) {
deleteFromFirst(head);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position out of range\n");
return;
}
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}
// Function to print the LinkedList
void print(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// 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);
printf("Linked list after inserting the node:20 at the end \n");
insertAtEnd(&head, 20);
print(head);
printf("Linked list after inserting the node:5 at the end \n");
insertAtEnd(&head, 5);
print(head);
printf("Linked list after inserting the node:30 at the end \n");
insertAtEnd(&head, 30);
print(head);
printf("Linked list after inserting the node:15 at position 2 \n");
insertAtPosition(&head, 15, 2);
print(head);
printf("Linked list after deleting the first node: \n");
deleteFromFirst(&head);
print(head);
printf("Linked list after deleting the last node: \n");
deleteFromEnd(&head);
print(head);
printf("Linked list after deleting the node at position 1: \n");
deleteAtPosition(&head, 1);
print(head);
return 0;
}
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 -> NULLExplanation
This program implements a singly linked list in C using structures and dynamic memory allocation.
- It supports insertion, deletion, and traversal operations.
- malloc() is used to create nodes dynamically, while free() releases their memory.
- The main() function demonstrates these operations on the linked list.