Singly Linked List In C

A format for every node in the list needs to be established to describe a singular linked list in the C programming language. This format should consist of two components: a data section to store the specific information and a pointer section to retain a connection to the next node.

Example

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

We need to start by setting the head pointer to NULL, indicating that the list is empty, to create a singly linked list. Subsequently, we can append nodes to the list by allocating memory dynamically for each node and linking them together using the next pointer.

Example

struct Node* head = NULL;  // Initializing an empty list

void insert(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

    if (head == NULL) {
        head = newNode;
    } else {
        struct Node* current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

We should start by setting the head pointer to NULL, indicating that the list is currently empty, to create a singly linked list. Following this step, nodes can be inserted into the list by dynamically allocating memory for each node and linking them using the next pointer.

Example

void traverse() {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
}

A singly linked list can be explored to find a specific element by moving through the list and checking each node's data against the target value. When a match is found, the corresponding node is returned; otherwise, NULL is returned to indicate that the element is not found in the list.

Example

struct Node* search(int value) {
    struct Node* current = head;
    while (current != NULL) {
        if (current->data == value) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

To eliminate a component from a singly linked list, you need to identify the node containing the target value and adjust the connections to the previous and next nodes. It's important to release the memory occupied by the removed node.

Example

void delete(int value) {
    if (head == NULL) {
        printf("List is empty.");
        return;
    }

    struct Node* current = head;
    struct Node* previous = NULL;

    while (current != NULL) {
        if (current->data == value) {
            if (previous == NULL) {
                head = current->next;
            } else {
                previous->next = current->next;
            }
            free(current);
            return;
        }
        previous = current;
        current = current->next;
    }

    printf("Element not found in the list.");
}

Example:

Let's generate a list, insert elements, and execute different functions on it to demonstrate the utilization of a singly linked list.

Example

#include <stdio.h>
#include <stdlib.h>

// Singly Linked List structure
struct Node {
    int data;
    struct Node* next;
};

// Global head pointer
struct Node* head = NULL;

// Function to insert a node at the end of the list
void insert(int value) {
    // Create a new node
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;

    // Check if the list is empty
    if (head == NULL) {
        head = newNode;
    } else {
        // Traverse to the end of the list
        struct Node* current = head;
        while (current->next != NULL) {
            current = current->next;
        }
        // Link the new node to the last node
        current->next = newNode;
    }
}

// Function to traverse and print the list
void traverse() {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
}

int main() {
    // Insert elements into the list
    insert(10);
    insert(20);
    insert(30);
    insert(40);

    // Traverse and print the list
    printf("List: ");
    traverse();

    return 0;
}
Example

List: 10 20 30 40

Input Required

This code uses input(). Please provide values below: