In the following linked list creation program, after input, the program stops working. The code after printf("\n nodes entered are:\n)
is not running.
The if in for loop is used for creation of head or start node.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
//creating a linked list
typedef struct node
{
int data;
struct node *link;
}node;
int main()
{
int i,n;
node* temp;
node* start=0;
printf("Enter the no of elements in the linked list\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i==0) //for first node
{
node* start=(node*)malloc(sizeof(node));
scanf("%d",&(start->data));
start->link=NULL;
temp=start;
}
else
{
node *nextnode=(node *)malloc(sizeof(node));
scanf("%d",&(nextnode->data));
temp->link=nextnode;
nextnode->link=NULL;
temp=nextnode; //updating temp for next iteration
}
}
printf("\n nodes entered are:\n");
temp=start;
while(temp->link!=NULL)
{
printf("%d ",temp->data);
temp=temp->link;
}
printf("%d",temp->data);
getch();
return 0;
}