The program takes input before asking for it. The problem starts after we input the value for first node.
This is a simple program which takes input from user and stores it in a linked list and then displays the data stored.
#include<stdio.h>
#include<stdlib.h>
struct NODE
{
int data;
struct NODE* next;
};
void main()
{
struct NODE *first,*old,*new_node;
int n,i;
printf ("Enter number of elements: \n");
scanf ("%d",&n);
first=(struct NODE*)malloc(sizeof(struct NODE));
first->next= NULL;
printf ("Enter value of node 1: \n");
scanf ("%d\n",&first->data);
old = first;
for(i=2;i<=n;i++)
{
new_node=(struct NODE*)malloc(sizeof(struct NODE));
new_node->next= NULL;
printf("Enter value of node %d: \n",i);
scanf("%d\n",&new_node->data);
old->next=new_node;
old = new_node;
}
old=first;
while(old!= NULL)
{
printf("%d \t",old->data);
old=old->next;
}
}