#include<stdio.h>
struct node
{
int item;
struct node *link
};
main()
{
struct node *start,*list;
int i;
start = (struct node *)malloc(sizeof(struct node));
list = start;
start->link = NULL;
for(i=0;i<10;i++)
{
list->item = i;
list->link = (struct node *)malloc(sizeof(struct node));
}
list->link = NULL;
while(start != NULL)
{
printf("%d\n",start->item);
start = start->link;
}
}
As the title suggests in this i am trying to traverse through a linked list itteratively
the expected output is
0
1
.
.
9
and the observed output is : 9
What is wrong with the code ?
It is just because of one statement in your code.
You forgot to point to the next link, when you tried to allocate the new link.
Because of that you were allocating only at one pointer, thus having memory leak.