Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct listNode
- {
- int value;
- struct listNode *next;
- };
- struct listNode *addToList(struct listNode *p, int v)
- {
- if (p == NULL)
- {
- p = malloc(sizeof(struct listNode));
- p->value = v;
- p->next = NULL;
- }
- else
- {
- p->next = addToList(p->next, v);
- }
- return p;
- }
- void printList(struct listNode *p)
- {
- while (p != NULL)
- {
- printf("%d\n", p->value);
- p = p->next;
- }
- }
- int main()
- {
- FILE *f = fopen("in.txt", "r");
- int v;
- struct listNode *root = NULL;
- while (fscanf(f, "%d\n", &v) != EOF)
- {
- root = addToList(root, v);
- }
- printList(root);
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment