I am trying to construct an linked list of 5 members in the following snippet but the snipet just prints two elements. Can anybody point out where am I going wrong:
Code:
int main(int argc, char **argv)
{
struct node *head, *curr, *temp;
int i;
head = (struct node*)malloc(sizeof(struct node));
curr = head;
for(i=1;i<=5;i++)
{
curr -> next = (struct node*)malloc(sizeof(struct node));
curr -> x= i;
temp=curr;
curr = curr->next;
}
temp->next =0;
//Display members
curr = head;
if(curr->next!=0)
{
printf("\n %d", curr->x);
curr = curr->next;
}
printf("\n %d", curr->x); // to print the last element in the list
return 0;
}