Source.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. struct onelist_list {
  4. int id;
  5. struct onelist_list* new_elem;
  6. };
  7. typedef struct onelist_list list;
  8. list* create_list(int count_elem) {
  9. list* first_elem = (list*)malloc(sizeof(list));
  10. first_elem->id = 0;
  11. list* post_elem_list, * next_elem_eist;
  12. post_elem_list = first_elem;
  13. for (size_t i = 0; i < count_elem - 1; i++)
  14. {
  15. next_elem_eist = (list*)malloc(sizeof(list));
  16. next_elem_eist->id = post_elem_list->id + 1;
  17. post_elem_list->new_elem = next_elem_eist;
  18. post_elem_list = next_elem_eist;
  19. }
  20. post_elem_list->new_elem = NULL;
  21. return first_elem;
  22. }
  23. void delete_list(list* one_list) {
  24. list* temp_list = one_list;
  25. while (temp_list) {
  26. temp_list = one_list->new_elem;
  27. free(one_list);
  28. one_list = temp_list;
  29. }
  30. }
  31. list* input_in_list(list* one_list, int index, int value) {
  32. list* new_elem = (list*)malloc(sizeof(list));
  33. list* head = one_list;
  34. new_elem->id = value;
  35. new_elem->new_elem = NULL;
  36. if (index == 0) {
  37. new_elem->new_elem = one_list;
  38. return new_elem;
  39. }
  40. int count = 0;
  41. while (one_list) {
  42. if (count == index - 1) {
  43. list* temp_elem = one_list->new_elem;
  44. one_list->new_elem = new_elem;
  45. new_elem->new_elem = temp_elem;
  46. break;
  47. }
  48. else {
  49. one_list = one_list->new_elem;
  50. count++;
  51. }
  52. }
  53. one_list = head;
  54. return one_list;
  55. }
  56. list* delete_in_list(list* one_list, int index) {
  57. list* head = one_list;
  58. if (index == 0) {
  59. head = one_list->new_elem;
  60. free(one_list);
  61. one_list = head;
  62. return one_list;
  63. }
  64. int count = 0;
  65. while (one_list) {
  66. if (count == index - 1) {
  67. list* temp_elem = one_list->new_elem->new_elem;
  68. free(one_list->new_elem);
  69. one_list->new_elem = temp_elem;
  70. break;
  71. }
  72. else {
  73. one_list = one_list->new_elem;
  74. count++;
  75. }
  76. }
  77. /*if (index == count_elem || index == count_elem - 1) {
  78. for (size_t i = 0; i < count_elem; i++)
  79. {
  80. if (i == count_elem - 2) {
  81. free(one_list->new_elem);
  82. one_list->new_elem = NULL;
  83. one_list = head;
  84. return one_list;
  85. }
  86. else {
  87. one_list = one_list->new_elem;
  88. }
  89. }
  90. }
  91. for (size_t i = 0; i < count_elem; i++)
  92. {
  93. if (i == index - 1) {
  94. list* temp_elem = one_list->new_elem->new_elem;
  95. free(one_list->new_elem);
  96. one_list->new_elem = temp_elem;
  97. break;
  98. }
  99. else {
  100. one_list = one_list->new_elem;
  101. }
  102. }*/
  103. one_list = head;
  104. return one_list;
  105. }
  106. list* change_in_list(list* one_list, int index_first, int index_second) {
  107. if (index_first > index_second) {
  108. int temp = index_first;
  109. index_first = index_second;
  110. index_second = temp;
  111. }
  112. list* head = one_list;
  113. list* first_elem = malloc(sizeof(list));
  114. list* second_elem = malloc(sizeof(list));
  115. int count = 0;
  116. while (one_list) {
  117. if (count == index_first)
  118. {
  119. first_elem = one_list;
  120. break;
  121. }
  122. else {
  123. one_list = one_list->new_elem;
  124. count++;
  125. }
  126. }
  127. one_list = head;
  128. count = 0;
  129. while (one_list) {
  130. if (count == index_second)
  131. {
  132. second_elem = one_list;
  133. break;
  134. }
  135. else {
  136. one_list = one_list->new_elem;
  137. count++;
  138. }
  139. }
  140. one_list = head;
  141. if (index_first != 0) {
  142. list* temp_elem = first_elem->new_elem;
  143. first_elem->new_elem = second_elem->new_elem;
  144. second_elem->new_elem = temp_elem;
  145. count = 0;
  146. while (one_list) {
  147. if (count == index_first - 1) {
  148. one_list->new_elem = second_elem;
  149. break;
  150. }
  151. else {
  152. one_list = one_list->new_elem;
  153. count++;
  154. }
  155. }
  156. one_list = head;
  157. count = 0;
  158. while (one_list) {
  159. if (count > index_first && one_list->new_elem->id == second_elem->id) {
  160. one_list->new_elem = first_elem;
  161. break;
  162. }
  163. else {
  164. one_list = one_list->new_elem;
  165. count++;
  166. }
  167. }
  168. }
  169. else {
  170. list* temp_elem = second_elem->new_elem;
  171. second_elem->new_elem = head->new_elem;
  172. head = second_elem;
  173. one_list = head;
  174. count = 0;
  175. while (one_list) {
  176. if (count != 0 && one_list->new_elem->id == second_elem->id) {
  177. one_list->new_elem = first_elem;
  178. first_elem->new_elem = temp_elem;
  179. break;
  180. }
  181. else {
  182. one_list = one_list->new_elem;
  183. count++;
  184. }
  185. }
  186. }
  187. one_list = head;
  188. return one_list;
  189. }
  190. list* print_list(list* one_list) {
  191. list* head = one_list;
  192. while(one_list) {
  193. printf_s("%d ", one_list->id);
  194. one_list = one_list->new_elem;
  195. }
  196. return head;
  197. }
  198. int main() {
  199. list* one_list = create_list(10);
  200. one_list = print_list(one_list);
  201. printf_s("\n");
  202. //delete_list(one_list);
  203. one_list = input_in_list(one_list, 0, 20);
  204. one_list = input_in_list(one_list, 7, 20);
  205. one_list = input_in_list(one_list, 7, 20);
  206. one_list = input_in_list(one_list, 7, 20);
  207. one_list = input_in_list(one_list, 7, 20);
  208. one_list = input_in_list(one_list, 7, 20);
  209. one_list = input_in_list(one_list, 7, 20);
  210. one_list = input_in_list(one_list, 7, 20);
  211. one_list = input_in_list(one_list, 7, 20);
  212. one_list = print_list(one_list);
  213. printf_s("\n");
  214. one_list = delete_in_list(one_list, 5);
  215. one_list = print_list(one_list);
  216. printf_s("\n");
  217. one_list = change_in_list(one_list, 1, 0);
  218. one_list = print_list(one_list);
  219. printf_s("\n");
  220. }