Guest User

Untitled

a guest
May 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct list
  5. {
  6.     int value;
  7.     struct list *next;
  8. };
  9.  
  10. void print_list(struct list *input)
  11. {
  12.     while (input)
  13.     {
  14.         printf("%i ", input->value);
  15.         input = input->next;
  16.     }
  17. }
  18.  
  19. void free_list(struct list *input)
  20. {
  21.     if (input)
  22.     {
  23.         free_list(input->next);
  24.         free(input);
  25.     }
  26. }
  27.  
  28. struct list *add_to_list(struct list *input, int value)
  29. {
  30.     if (!input)
  31.     {
  32.         input = (struct list *) calloc(1, sizeof (struct list));
  33.         input->value = value;
  34.         return input;
  35.     }
  36.     else if (input)
  37.     {
  38.         struct list *temp = NULL;
  39.         if (value < input->value)
  40.         {
  41.             temp = (struct list *) calloc(1, sizeof (struct list));
  42.             temp->next = input;
  43.             temp->value = value;
  44.             return temp;
  45.         }
  46.         else if (value >= input->value)
  47.         {
  48.             struct list *cur = input->next;
  49.             ;
  50.             struct list *prev = input;
  51.             temp = input;
  52.             while (input->next && value > input->next->value && value != input->next->value)
  53.             {
  54.                 input = input->next;
  55.                 prev = input;
  56.                 cur = input->next;
  57.             }
  58.             if (!cur)
  59.             {
  60.                 cur = (struct list *) calloc(1, sizeof (struct list));
  61.                 cur->value = value;
  62.                 prev->next = cur;
  63.             }
  64.             else
  65.             {
  66.                 prev->next = (struct list *) calloc(1, sizeof (struct list));
  67.                 prev->next->next = cur;
  68.                 prev->next->value = value;
  69.             }
  70.             return temp;
  71.         }
  72.     }
  73. }
  74.  
  75. int main()
  76. {
  77.     FILE *input = fopen("input.txt", "r");
  78.     struct list *worklist = NULL;
  79.     int temp = 0;
  80.  
  81.     if (input == NULL)
  82.     {
  83.         printf("ERROR!!! Programm Cannot open input file\n");
  84.         return -1;
  85.     }
  86.     if (feof(input))
  87.     {
  88.         printf("empty\n");
  89.         fclose(input);
  90.         return -1;
  91.     }
  92.  
  93.     while (1)
  94.     {
  95.         if (fscanf(input, "%i", &temp) != 1)
  96.         {
  97.             if (feof(input))
  98.             {
  99.                 printf("Input file is read\n");
  100.             }
  101.             else
  102.             {
  103.                 printf("ERROR!!! Programm find a non-integer number in input file\n");
  104.             }
  105.             break;
  106.         }
  107.         worklist = add_to_list(worklist, temp);
  108.     }
  109.     print_list(worklist);
  110.     free_list(worklist);
  111.     return 0;
  112. }
Add Comment
Please, Sign In to add comment