struct node
{
int value;
struct node* nextptr;
};
typedef struct node Node;
typedef struct node* Nodeptr;
Nodeptr* currentptr = NULL;
Nodeptr* previousptr = NULL;
Nodeptr* startptr =NULL;
void insertnode(int data)
{
Nodeptr newptr;
newptr = (Nodeptr)malloc(sizeof(Node));
newptr->value = data;
newptr-> nextptr = NULL;
(*currentptr) = (Nodeptr)malloc(sizeof(Node));
(*previousptr) = (Nodeptr)malloc(sizeof(Node));
(*startptr) = (Nodeptr)malloc(sizeof(Node));
if((*currentptr) == NULL)
{
*currentptr = newptr;
*startptr = newptr;
}
else
{
(*currentptr)->nextptr = newptr;
*previousptr = *currentptr;
(*currentptr) = newptr;
}
}
This is the code that I'm using to create the linked lists.
for(int i=0;i<n;i++)
{
int num;
scanf("%d",&num);
insertnode(num);
}
This is used to get input from the user using a for loop.. int n is declared & initialized already. My program crashes after getting the first input. I have thought of memory allocation problems but that does not seem to resolve the problem. Also I'm new to C & programming so please excuse any tiny mistakes!