So I was trying my hands on a linked list in c, although I think I have done all well, my list is not being loaded when I try to load it...can somone point me to where I'm going wrong? now I post the snippet.. when I try to print out the items in the list nothing is printed out and when I try to count the items I get a count of zero...so I'm surely doing something wrong ...
Code:
typedef struct n
{
char name [100];
//double price;
struct n * next;
} node;
void loadItemsIntoList(node * testa, char nome [] );
int main()
{
node * list = 0;
//random list loading..
loadItemsIntoList(list, "fff");
loadItemsIntoList(list, "ssss");
loadItemsIntoList(list, "sdfsd");
loadItemsIntoList(list, "lknou");
for (node * curr = list; curr != 0; curr = curr->next)
{
printf("%s", curr->name);
}
//testing for length of list
int count = 0;
for(node * curr = list; curr != 0; curr = curr->next)
{
count += 1;
}
printf("\nlength of node is %d ,", count );
return 0;
}
void loadItemsIntoList(node * testa, char nome [] /*, double prezzo */)
{
node * temp = malloc(sizeof(node));
strcpy(temp->name, nome);
//temp->price = prezzo;
temp->next = 0;
if(testa == 0) { testa = temp; }
else{
node * curr = testa;
while(curr->next != 0){curr = curr->next;}
curr->next = temp;
}
}