I had written the code just to create a linked list but it's showing segmentation fault. Can anybody tell me what's the mistake and what should I do to resolve it
My code for the singly linked list
// defining the linked list (basics)
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
void create(struct node **head, int data)
{
struct node *temp;
if (*head == NULL)
{
*head = (struct node*)malloc(sizeof(struct node));
(*head) -> data = data;
(*head) -> next = NULL;
}
else
{
temp = *head;
while (temp != NULL)
{
temp = temp -> next;
}
temp = (struct node*)malloc(sizeof(struct node));
temp -> data = data;
temp -> next = NULL;
}
}
int main()
{
struct node *begin = NULL;
create(&begin, 1);
create(&begin, 7);
printf("%d\n", begin -> next -> data);
printf("%lu\n", sizeof(struct node));
return 0;
}