Single Linked List Introduction in Data Structure using C

In C, a singly linked list is a data structure that consists of a sequence of nodes. Each node contains an element and a pointer to the next node in the sequence. Here is an example of how to create and manipulate a singly linked list in C:

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

// Define the node structure
typedef struct node {
    int data;
    struct node* next;
} Node;

// Define the linked list structure
typedef struct linked_list {
    Node* head;
    int size;
} LinkedList;

// Create a new node with the specified data

Node* create_node(int data) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

// Add a new node with the specified data to the end of the list
void add_node(LinkedList* list, int data) {
    Node* new_node = create_node(data);
    if (list->head == NULL) {
        list->head = new_node;
    } else {
        Node* current = list->head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_node;
    }
    list->size++;
}

// Print the contents of the list

void print_list(LinkedList* list) {
    Node* current = list->head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}


// Free the memory allocated for the list
void free_list(LinkedList* list) {
    Node* current = list->head;
    while (current != NULL) {
        Node* temp = current;
        current = current->next;
        free(temp);
    }
    free(list);
}


int main() {
    LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList)); // Create a new linked list
    list->head = NULL;

    list->size = 0;

    add_node(list, 1); // Add some nodes to the list
    add_node(list, 2);
    add_node(list, 3);

    print_list(list); // Print the contents of the list

    free_list(list); // Free the memory allocated for the list

    return 0;
}
```

In this example, we first define a node structure that contains an integer data element and a pointer to the next node in the list. We then define a linked list structure that contains a pointer to the head node of the list and the size of the list.

We provide functions to create a new node with a specified data element, add a new node to the end of the list, print the contents of the list, and free the memory allocated for the list.

In the `main()` function, we create a new linked list, add some nodes to the list, print the contents of the list, and then free the memory allocated for the list.

Note that this example only scratches the surface of what is possible with singly linked lists in C. For more information and examples, consult the C documentation and tutorials.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext