i'm having troubles with this, it compiles fine, but doesn't printf anything, it seems that no nodes are added to the list at all
Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int dat;
struct node *next;
};
typedef struct node* link;
void addnode(link *list, int d);
void delall(link *list);
int count(link list);
int main(void)
{
link root = NULL, index;
int x;
for (x = 0; x < 5; x++)
addnode(&root, x);
for (index = root; index != NULL; index = index->next)
printf("%d ", index->dat);
putchar('\n');
printf("%d nodes\n", count(root) );
delall(&root);
return 0;
}
void addnode(link *list, int d)
{
link new;
link cur = *list;
new = malloc( sizeof(*new) );
if (new == NULL)
exit(1);
else
new->dat = d;
while (cur != NULL)
cur = cur->next;
cur = new;
new->next = NULL;
}
void delall(link *list)
{
link cur = *list, sav;
while (cur != NULL)
{
sav = cur->next;
free(cur);
cur = sav;
}
}
int count(link list)
{
link cur = list;
int c = 0;
while (cur != NULL)
{
cur = cur->next;
c++;
}
return c;
}