I am a beginner in data structure and I recently wrote a code to insert a new node at the beginning of a linked list using functions but when I run it the inserted node doesn't get printed, please help me verify the error in my code and correct it
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void
traversal(struct Node *ptr)
{
while (ptr != NULL) {
printf("%d\n", ptr->data);
ptr = ptr->next;
}
}
void
insert_first(struct Node *head, int data)
{
struct Node *ptr = (struct Node *) malloc(sizeof(struct Node));
ptr->data = data;
ptr->next = head;
head = ptr;
}
int
main()
{
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
head = (struct Node *) malloc(sizeof(struct Node));
second = (struct Node *) malloc(sizeof(struct Node));
third = (struct Node *) malloc(sizeof(struct Node));
fourth = (struct Node *) malloc(sizeof(struct Node));
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = fourth;
fourth->data = 40;
fourth->next = NULL;
traversal(head);
insert_first(head, 0);
printf("\n");
traversal(head);
return 0;
}