Linked Lists in C

 Linked Lists


Linked lists are dynamic data structures that allow efficient insertion and deletion of elements. They consist of nodes containing data and a pointer to the next node. Implementing linked lists enhances your understanding of data structures and memory management.



#include <stdio.h>

#include <stdlib.h>


struct Node {

    int data;

    struct Node* next;

};


void insert(struct Node** head, int data) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->next = *head;

    *head = newNode;

}


void display(struct Node* head) {

    struct Node* current = head;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

    printf("\n");

}


int main() {

    struct Node* head = NULL;

    insert(&head, 3);

    insert(&head, 2);

    insert(&head, 1);

    display(head);


    return 0;

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...