‹ ›
XmonoCodes • Learn / Build / Grow

CREATIVITY
MEETS CODE.

Practical knowledge for students, developers and curious builders — from programming and web development to projects, notes and real-world problem solving.

  • “Learn something useful every day.”
  • “Build ideas into real projects.”
  • “Understand the why, not just the how.”
  • “Keep experimenting. Keep improving.”

Latest from XmonoCodes

Explore tutorials, notes, projects, syllabus material and practical coding content.

Monday, February 24, 2025

Insert at beginning in Linked list

 #include <stdio.h>

#include <stdlib.h>


struct Node {

    int data;

    struct Node* next;

};


void insertAtBeginning(struct Node** head, int value) {

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

    newNode->data = value;

    newNode->next = *head;

    *head = newNode;

}


void printList(struct Node* head) {

    while (head != NULL) {

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

        head = head->next;

    }

    printf("NULL\n");

}


int main() {

    struct Node* head = NULL;


    insertAtBeginning(&head, 10);

    insertAtBeginning(&head, 20);

    insertAtBeginning(&head, 30);


    printList(head);


    return 0;

}

No comments:

Post a Comment