Advertisement
constk

Lab_Lists

Mar 16th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <ctime>
  5.  
  6. typedef struct NODE
  7. {
  8. int inf;
  9. NODE* link;
  10. };
  11.  
  12. void createList(NODE*, const size_t size);//Создать список, проинициализировать его нулями
  13. void initList(NODE*, int min, int max);//Проинициализировать список случайными значениями от min до max
  14. void inputList(NODE*);//Ввести список с клавиатуры
  15. void showList(NODE*);//Вывести список на экран
  16.  
  17. //Процедура из задания 4 - вставляет указанный элемент после каждого узла, значение которого превышает заданное число p
  18. int proc(NODE*, int, const int p);//Возвращает количество вставленных элементов
  19.  
  20. int main()
  21. {
  22. setlocale(0, "Russian");
  23. srand(time(NULL));
  24.  
  25. NODE* head = new NODE;
  26. size_t size = 0;
  27. printf("Введите размер списка: ");
  28. scanf("%d", &size);
  29.  
  30. createList(head, size);
  31. inputList(head);
  32. showList(head);
  33.  
  34. int element, p;
  35. printf("Введите элемент для вставки: ");
  36. scanf("%d", &element);
  37. printf("Введите p для сравнения: ");
  38. scanf("%d", &p);
  39.  
  40. int n = proc(head, element, p);
  41. showList(head);
  42. printf("Добавлено %d элементов", n);
  43.  
  44. system("pause");
  45. return 0;
  46. }
  47.  
  48. void createList(NODE* head, const size_t size)
  49. {
  50. NODE* tmp = NULL;
  51. NODE* current = head;
  52.  
  53. current->inf = 0;
  54. current->link = NULL;
  55.  
  56. for (int i = 1; i != size; ++i)
  57. {
  58. tmp = new NODE;
  59. tmp->inf = 0;
  60. tmp->link = NULL;
  61.  
  62. current->link = tmp;
  63. current = tmp;
  64. }
  65. }
  66.  
  67. void inputList(NODE* head)
  68. {
  69. NODE* current = head;
  70. do
  71. {
  72. printf("Введите элемент: ");
  73. scanf("%d", &current->inf);
  74. current = current->link;
  75. } while (current);
  76. }
  77.  
  78. void initList(NODE* head, int min, int max)
  79. {
  80. NODE* current = head;
  81. do
  82. {
  83. current->inf = rand() % (max - min + 1) + min;
  84. current = current->link;
  85. } while (current);
  86. }
  87.  
  88. void showList(NODE* head)
  89. {
  90. if (!head)
  91. {
  92. puts("Список пуст");
  93. return;
  94. }
  95. NODE* current = head;
  96. do
  97. {
  98. printf("%3d", current->inf);
  99. current = current->link;
  100. } while (current);
  101. puts("");
  102. }
  103.  
  104. int proc(NODE* head, int element, const int p)
  105. {
  106. if (!head)
  107. {
  108. puts("Список пуст");
  109. return 0;
  110. }
  111. NODE* current = head;
  112. NODE* tmp = NULL;
  113. unsigned count = 0;
  114. do
  115. {
  116. if (current->inf > p)
  117. {
  118. tmp = new NODE;
  119. tmp->inf = element;
  120. tmp->link = current->link;
  121. current->link = tmp;
  122. count++;
  123. }
  124. current = current->link;
  125. } while (current);
  126. return count;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement