Advertisement
dxvmxnd

кпо_список

Nov 26th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #pragma hdrstop
  2. #pragma argsused
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. typedef char _TCHAR;
  8. #define _tmain main
  9.  
  10.  
  11. struct Node {
  12. int num;
  13. struct Node* next;
  14. };
  15.  
  16. struct Node* head = NULL;
  17.  
  18.  
  19. void freeList() {
  20. struct Node* temp;
  21. while (head != NULL) {
  22. temp = head;
  23. head = head->next;
  24. free(temp);
  25. }
  26. printf("Список очищен.\n");
  27. }
  28.  
  29. void appendInList(int num) {
  30.  
  31. struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  32. struct Node* last = head;
  33.  
  34. new_node->num = num;
  35.  
  36. new_node->next = NULL;
  37.  
  38. // если пустой, список в голову
  39. if (head == NULL) {
  40. head = new_node;
  41. return;
  42. }
  43.  
  44. // проход по списку
  45. while (last->next != NULL) {
  46. last = last->next;
  47. }
  48.  
  49. // переход на новый узел
  50. last->next = new_node;
  51. }
  52.  
  53. void inputBase() {
  54. srand(time(NULL));
  55. for (int i = 0; i < 20; i++) {
  56. int random_number = rand() % 100 + 1;
  57. appendInList(random_number);
  58. }
  59.  
  60. }
  61.  
  62.  
  63. void checkBase() {
  64. struct Node* current = head;
  65.  
  66.  
  67. if (current == NULL) {
  68. printf("База данных пуста.\n");
  69. return;
  70. }
  71.  
  72. printf("Вывод чисел:");
  73.  
  74.  
  75. while (current != NULL) {
  76. printf("%d ", current->num);
  77. current = current->next;
  78. }
  79. }
  80.  
  81. void outputBase() {
  82. struct Node* current = head;
  83. struct Node* lastEven = NULL;
  84.  
  85. while (current != NULL) {
  86. if (current->num % 2 == 0) {
  87. lastEven = current;
  88. }
  89. current = current->next;
  90. }
  91.  
  92.  
  93. current = head;
  94. struct Node* new_node;
  95. while (current != NULL) {
  96. if (current->num % 2 != 0) {
  97. new_node = (struct Node*)malloc(sizeof(struct Node));
  98. new_node->num = lastEven->num;
  99. new_node->next = current->next;
  100. current->next = new_node;
  101. current = new_node->next;
  102. } else {
  103. current = current->next;
  104. }
  105. }
  106.  
  107.  
  108. checkBase();
  109.  
  110. }
  111.  
  112. void chooseProc() {
  113. int choose;
  114.  
  115. printf("\n1: Посмотреть базу\n");
  116. printf("2: Вывести (по заданию)\n");
  117. printf("3: Выход\n");
  118.  
  119. scanf("%d", &choose);
  120. switch (choose) {
  121. case 1:
  122. checkBase();
  123. break;
  124. case 2:
  125. outputBase();
  126. break;
  127. case 3:
  128. exit(0);
  129. break;
  130. default:
  131. printf("Ошибка\n");
  132. break;
  133. ;
  134. }
  135. chooseProc();
  136. }
  137.  
  138. int _tmain(int argc, _TCHAR* argv[])
  139. {
  140. system("chcp 1251");
  141. inputBase();
  142. chooseProc();
  143. return 0;
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement