Linked List Implementation in C: Step-by-Step Explanation

Introduction:

Linked lists are fundamental data structures widely used in computer science and programming. In this guide, we’ll walk through a simple implementation of a linked list in the C programming language. This explanation will be beginner-friendly, breaking down each line of code to understand how linked lists work and how to manipulate them.

Let’s dive in!

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

// Creating a node
struct node {
    int value;
    struct node *next;
};

int main() {
    // Initialize nodes
    struct node *head;
    struct node *one = NULL;
    struct node *two = NULL;
    struct node *three = NULL;

    // Allocate memory
    one = malloc(sizeof(struct node));
    two = malloc(sizeof(struct node));
    three = malloc(sizeof(struct node));

    // Assign value values
    one->value = 1;
    two->value = 2;
    three->value = 3;

    // Connect nodes
    one->next = two;
    two->next = three;
    three->next = NULL;

    head = one;

    // printing node-value
    struct node *current = head;

    while (current != NULL) {
        printf("%d ", current->value); // print current node's value
        current = current->next; // move to the next node
    }

    return 0;
}

Now, let’s break down the code step by step:

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

These lines include necessary header files. <stdio.h> is for input and output operations like printf, and <stdlib.h> is for functions like malloc, which is used to allocate memory dynamically.

struct node {
    int value;
    struct node *next;
};

This defines a structure named node. Each node has an integer value (value) and a pointer to another node (next), which represents the next node in the linked list.

int main() {
    // Initialize nodes
    struct node *head;
    struct node *one = NULL;
    struct node *two = NULL;
    struct node *three = NULL;

Here, we’re declaring pointers to nodes. head will point to the first node of the linked list, and one, two, and three will point to individual nodes.

    // Allocate memory
    one = malloc(sizeof(struct node));
    two = malloc(sizeof(struct node));
    three = malloc(sizeof(struct node));

This allocates memory for each node using malloc. It allocates memory equivalent to the size of the struct node for each node pointer (one, two, and three).

    // Assign value values
    one->value = 1;
    two->value = 2;
    three->value = 3;

Here, we’re assigning values to the value member of each node. one gets 1, two gets 2, and three gets 3.

    // Connect nodes
    one->next = two;
    two->next = three;
    three->next = NULL;

This part links the nodes together. one->next is assigned to two, two->next is assigned to three, and three->next is set to NULL to indicate the end of the list.

    head = one;

This sets the head pointer to point to the first node of the linked list, which is one.

    // printing node-value
    struct node *current = head;

    while (current != NULL) {
        printf("%d ", current->value); // print current node's value
        current = current->next; // move to the next node
    }

Here, we’re traversing the linked list starting from head. Inside the while loop, we print the value of the current node and then move to the next node by updating current to point to the next node (current->next).

Finally, we return 0 to indicate successful completion of the program.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top