Advertisement
Galebickosikasa

Untitled

Dec 27th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node {
  6. int value;
  7. struct node* back;
  8. struct node* next;
  9. };
  10.  
  11. struct node* init_node (int x) {
  12. struct node* n = malloc (sizeof (struct node));
  13. n->value = x;
  14. return n;
  15. }
  16.  
  17. struct list {
  18. struct node* begin;
  19. struct node* end;
  20. int size;
  21. };
  22.  
  23. struct list* init_list (void) {
  24. struct list* list = malloc (sizeof (struct list));
  25. list->size = 0;
  26. return list;
  27. }
  28.  
  29. void destruct_list (struct list* list) {
  30. if (list->size == 0) {
  31. free (list);
  32. return;
  33. } else if (list->size == 1) {
  34. free (list->begin);
  35. free (list);
  36. return;
  37. }
  38. for (struct node* node = list->begin->next; node != list->end; node = node->next) {
  39. free (node->back);
  40. }
  41. free (list->end->back);
  42. free (list->end);
  43. free (list);
  44. }
  45.  
  46. void push_back (struct list* list, int x) {
  47. struct node* new_node = init_node(x);
  48. if (list->size == 0) {
  49. list->begin = list->end = new_node;
  50. } else {
  51. list->end->next = new_node;
  52. new_node->back = list->end;
  53. list->end = new_node;
  54. }
  55. ++list->size;
  56. }
  57.  
  58. void sort (struct list* list) {
  59. for (int i = 0; i < list->size; ++i) {
  60. struct node* node = list->begin;
  61. for (int j = 0; j < list->size - 1; ++j) {
  62. if (node->value > node->next->value) {
  63. node->value ^= node->next->value;
  64. node->next->value ^= node->value;
  65. node->value ^= node->next->value;
  66. }
  67. node = node->next;
  68. }
  69. }
  70. }
  71.  
  72. int main (void) {
  73. FILE* in = fopen ("input.txt", "r");
  74. FILE* out = fopen ("output.txt", "w");
  75. struct list* list = init_list();
  76. while (1) {
  77. int x;
  78. fscanf(in, "%d", &x);
  79. if (feof(in)) break;
  80. push_back(list, x);
  81. }
  82. sort (list);
  83. struct node* node = list->begin;
  84. for (int i = 0; i < list->size; ++i) {
  85. fprintf(out, "%d ", node->value);
  86. node = node->next;
  87. }
  88. fclose(in);
  89. fclose(out);
  90. destruct_list(list);
  91.  
  92.  
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement