When I run this code it shows a Segmentation fault.
I have searched the other related post on Stackoverflow but didn't get the answer or why my code showing this error.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node{
int info;
struct node *next;
};
typedef struct node NODE;
NODE* getNode();
void insertAtFirst(NODE*,int);
void traverse(NODE*);
int main(){
NODE *start = NULL;
insertAtFirst(start,1);
insertAtFirst(start,4);
traverse(start);
return 0;
}
void insertAtFirst(NODE *start, int n){
NODE *p = (NODE*)malloc(sizeof(NODE));
p->info = n;
if(start == NULL){
p->next = NULL;
}
else{
p->next = start;
}
start = p;
}
void traverse(NODE *start){
NODE *temp;
temp = start;
while(temp != NULL){
printf("%d ", temp->info);
temp = temp->next;
}
}
Please suggest me why I am getting a Segmentation fault (core dump) on running the program.