C Program to Implement Singly Linked List (original) (raw)

Last Updated : 6 Aug, 2025

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.

Implementation of Singly Linked List in C

A singly linked list is a type of linked list where only the address of the next node is stored in the current node along with the data field and the last node in the list contains NULL pointer. This makes it impossible to find the address of the particular node in the list without accessing the node previous to it. So we can only access the data sequentially in the node.

Representation of Singly Linked List in C

To represent a node of a singly linked list in C, we will use a structure that will have two data members **data and **next where:

struct *Node {
int data;
Node
next;
};

where,

The following diagram represents the structure of a singly linked list:

Singly-Linked-List-(1)

The pointer to the first node of the list is called head.

Singly Linked List Basic Operations in C

Following are some of the basic operations which are required to manipulate the nodes of a singly linked list:

Operation Operation Type Description Time Complexity Space Complexity
**Insert At Head Appends a new node at the start of the singly linked list. O(1) O(1)
At Given Position Adds a new node at a specific position in the singly linked list. O(N) O(1)
At End Adds a new node to the end of the singly linked list. O(N) O(1)
**Delete From Head Removes the first node from the singly linked list. O(1) O(1)
From Given Position Deletes a node present at a specific position in the singly linked list. O(N) O(1)
From End Eliminates the last node of the singly linked list. O(N) O(1)
**Print - Iterates through the singly liked list and prints the value of data present in each node. O(N) O(1)

**Note: Here N represents the number of nodes in the linked list.

Insertion Operation Implementation

**1. Algorithm for insertAtFirst Function

Following is the algorithm to insert a node at the start of the singly linked list:

**2. Algorithm for insertAtPosition Function

Following is the algorithm to insert a node at a specific position of the singly linked list:

**3. Algorithm for insertAtEnd Function

Following is the algorithm to insert a node at the end of the singly linked list:

Deletion Operations in a Singly Liked List in C

**1. Algorithm for deleteFromFirst Function

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.
  2. Create a temporary pointer and point it to the current Head of the list.
  3. Update the current head of the singly linked list to the next node.
  4. Point the next pointer of the temporary node to NULL to detach it from the singly linked list.
  5. Delete the temporary node.

**2. Algorithm for deleteFromPosition Function

Following is the algorithm to delete a node from a specific position in a linked list:

**3. Algorithm for deleteFromEnd Function

Following is the algorithm to delete the last node of the linked list:

Algorithm for Print Function

Following is the algorithm to iterate through the singly linked list and print the value of data present in each node:

C Program to Implement Singly Linked List

The following program illustrates how we can implement a singly linked list in C:

C `

// // C Program for Implementation of 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 -> NULL

Following are some articles that you can also read to improve your knowledge about the linked list data structure: