I am trying to implement linked list in c. In the insertion of the element, if the head is not NULL, I am trying to add a node in the beginning of the linked list Here is my code
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head ;
void insert(int data){
struct Node* temp = (struct Node*) malloc(sizeof(struct Node));
temp->data = data;
if(head!=NULL){
temp = head;
head = temp;
}
temp -> next = NULL;
head = temp;
}
void print(){
struct Node* temp = head;
while(temp!=NULL){
printf("%d \n",temp->data);
temp = temp->next;
}
}
int main(){
head = NULL;
insert(2);
insert(3);
insert(5);
print();
return 0;
}
But on print function, I am getting only 2 as output. What might be the reason?