linked

C Linked List Basics

C Linked List Basics

A linked list is a linear data structure where elements are stored in nodes. Each node contains data and a pointer to the next node.

Creating a Linked List

#include 
#include 

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

int main() {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

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

    return 0;
}
    

This program creates a simple linked list with three nodes and prints the elements.