Basic linked list example

#include
#include
#include

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;
}

0 comments:

Post a Comment