For some reason, I am unable to print the entire linked list. Where could I be going wrong?
Please help. Thanks in advance.
The basic structure of the list.
struct node
{
int num;
struct node *next;
};
typedef struct node *list;
Main function.
int main()
{
int i, j, k, l;
list head = NULL, start = NULL, temp, p;
printf("Enter the number of nodes in the list: ");
scanf("%d", &k);
Formation of the linked list.
for(i=0;i<k;i++)
{
if (i==0)
{
start = (list) malloc(sizeof(struct node));
start->num = i;
head = start;
head->next = NULL;
}
else
{
temp = (list) malloc(sizeof(struct node));
temp->num = i;
head->next = temp;
head->next = NULL;
}
}
Printing the linked list.
p = start;
while(p != NULL)
{
printf("%d", p->num);
p = p->next;
}
return 0;
}
Aren’t you missing here something:
You are overriding your next
headwithNULL. You should have yourheadat first point totemp.Edit: By the way, you should rename
headtocurrent/lastor something like that. Else, one could easily swap the meaning ofheadandstart.