## Introduction

A singly linked list is a basic data structure in C programming. It is a collection of nodes where each node contains data & a pointer to the next node. This structure allows for dynamic memory usage and efficient insertions and deletions. Singly linked lists are useful for representing sequences of elements, such as lists or stacks.

In this article, we will learn how to construct a singly linked list in C, which will include creating nodes, inserting & deleting nodes, & traversing the list.

## How to Construct a Singly Linked List in C

A singly linked list in C is built using a series of nodes linked together through pointers. Each node typically contains at least two parts: data and a pointer to the next node. Now, let's learn how to construct a basic singly linked list. We will start from defining the node structure.

### Defining the Node Structure

First, we define a structure to represent each node in the list. This structure will include the data and a pointer to the next node.

```
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
```

### Creating a New Node

To add data to our list, we need to create new nodes. A function can be used to allocate memory for a new node and initialize it with data.

```
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Error creating a new node.\n");
exit(0);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
```

This function creates a new node with the provided data and sets the next pointer to NULL, indicating that the node does not yet link to any other node.

### Adding Nodes to the List

We can start our list by creating a head pointer when we create the first node. Subsequent nodes can be added using various strategies, such as at the front of the list or at the end.

```
void insertAtFront(Node** head, int data) {
Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}
void insertAtEnd(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
```

insertAtFront adds a new node at the beginning of the list, changing the head of the list to the new node. insertAtEnd traverses the list to find the last node and adds the new node after it.

The syntax for creating a node:

To create a node in a singly linked list, we use the following syntax:

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

Here, struct Node defines the structure of a node. Each node contains two fields:

- data: An integer value that stores the data of the node.

- next: A pointer to the next node in the list.

To create a new node, we allocate memory using the malloc() function & assign values to its data & next fields:

```
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
```

In this code :

- (struct Node*)malloc(sizeof(struct Node)) allocates memory for a new node using malloc().

- newNode->data = value; assigns the specified value to the data field of the new node.

- newNode->next = NULL; sets the next pointer of the new node to NULL, indicating that it is the last node in the list.

By using this syntax, we can create nodes & build a singly linked list by connecting them together using the next pointers.

### Insertion of a node

There are three ways to insert a node into a singly linked list:

- Insertion at the beginning

- Insertion at the end

- Insertion at a specific position

To insert a node at the beginning of the list, we follow these steps:

- Create a new node with the given data.

- Set the next pointer of the new node to the current head of the list.

- Update the head of the list to point to the new node.

Here's the code for inserting a node at the beginning:

```
void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
```

To insert a node at the end of the list, we follow these steps:

- Create a new node with the given data.

- Traverse the list to reach the last node.

- Set the next pointer of the last node to the new node.

Here's the code for inserting a node at the end:

```
void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
```

To insert a node at a specific position, we follow these steps:

### Create a new node with the given data.

- Traverse the list to reach the node just before the desired position.

- Set the next pointer of the new node to the next node of the current node.

- Update the next pointer of the current node to the new node.

Here's the code for inserting a node at a specific position:

```
void insertAtPosition(struct Node** head, int value, int position) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if (position == 1) {
newNode->next = *head;
*head = newNode;
return;
}
struct Node* temp = *head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Invalid position\n");
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
```