acobzew

list

May 6th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct listNode
  5. {
  6.     int value;
  7.     struct listNode *next;
  8. };
  9.  
  10. struct listNode *addToList(struct listNode *p, int v)
  11. {
  12.     if (p == NULL)
  13.     {
  14.         p = malloc(sizeof(struct listNode));
  15.         p->value = v;
  16.         p->next = NULL;
  17.     }
  18.     else
  19.     {
  20.         p->next = addToList(p->next, v);
  21.     }
  22.     return p;
  23. }
  24.  
  25. void printList(struct listNode *p)
  26. {
  27.     while (p != NULL)
  28.     {
  29.         printf("%d\n", p->value);
  30.         p = p->next;
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.     FILE *f = fopen("in.txt", "r");
  37.     int v;
  38.     struct listNode *root = NULL;
  39.     while (fscanf(f, "%d\n", &v) != EOF)
  40.     {
  41.         root = addToList(root, v);
  42.     }
  43.  
  44.     printList(root);
  45.  
  46.     fclose(f);
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment