Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct list
- {
- int value;
- struct list *next;
- };
- void print_list(struct list *input)
- {
- while (input)
- {
- printf("%i ", input->value);
- input = input->next;
- }
- }
- void free_list(struct list *input)
- {
- if (input)
- {
- free_list(input->next);
- free(input);
- }
- }
- struct list *add_to_list(struct list *input, int value)
- {
- if (!input)
- {
- input = (struct list *) calloc(1, sizeof (struct list));
- input->value = value;
- return input;
- }
- else if (input)
- {
- struct list *temp = NULL;
- if (value < input->value)
- {
- temp = (struct list *) calloc(1, sizeof (struct list));
- temp->next = input;
- temp->value = value;
- return temp;
- }
- else if (value >= input->value)
- {
- struct list *cur = input->next;
- ;
- struct list *prev = input;
- temp = input;
- while (input->next && value > input->next->value && value != input->next->value)
- {
- input = input->next;
- prev = input;
- cur = input->next;
- }
- if (!cur)
- {
- cur = (struct list *) calloc(1, sizeof (struct list));
- cur->value = value;
- prev->next = cur;
- }
- else
- {
- prev->next = (struct list *) calloc(1, sizeof (struct list));
- prev->next->next = cur;
- prev->next->value = value;
- }
- return temp;
- }
- }
- }
- int main()
- {
- FILE *input = fopen("input.txt", "r");
- struct list *worklist = NULL;
- int temp = 0;
- if (input == NULL)
- {
- printf("ERROR!!! Programm Cannot open input file\n");
- return -1;
- }
- if (feof(input))
- {
- printf("empty\n");
- fclose(input);
- return -1;
- }
- while (1)
- {
- if (fscanf(input, "%i", &temp) != 1)
- {
- if (feof(input))
- {
- printf("Input file is read\n");
- }
- else
- {
- printf("ERROR!!! Programm find a non-integer number in input file\n");
- }
- break;
- }
- worklist = add_to_list(worklist, temp);
- }
- print_list(worklist);
- free_list(worklist);
- return 0;
- }
Add Comment
Please, Sign In to add comment