I am working on singly linked list and unable to solve problem (I think the problem is in add function something with NULL pointer), the problem is it only adding first number to list and skipping rest of call to add function.
#include<stdlib.h>
#include<stdio.h>
struct node
{
int i;
struct node* next;
};
struct node* head = NULL;
int add(int val)
{
if(head == NULL)
{
head = (struct node*)malloc(sizeof(struct node));
head->i = val;
}
else
{
struct node* current = head;
while(current != NULL)
{
current = current->next;
}
current = (struct node*)malloc(sizeof(struct node));
current->i = val;
}
}
int print(struct node* first)
{
while(first != NULL)
{
printf("%d\n",first->i);
first = first->next;
}
}
int main()
{
add(36);
add(46);
add(97);
print(head);
return 0;
}