Basic linked list example in C

#include stdio.h
#include stdlib.h
#include string.h

struct llist
{
char *str;
struct llist *next;
};

int main(void)
{
char line[1024];
struct llist *head = NULL;
struct llist *new = NULL;

while(fgets(line, 1024, stdin) != NULL)
{
new = (struct llist *)malloc(sizeof(struct llist));
new->next = head;
head = new;

new->str = strdup(line);
}

while(head != NULL)
{
printf("%s\n", head->str);
head = head->next;
}

return 0;
}

1 comment: