File.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. struct MyStruct
  6. {
  7. int a;
  8. struct MyStruct* next;
  9. };
  10. typedef struct MyStruct s;
  11. s* create(int n) {
  12. s* start = malloc(sizeof(s));
  13. start->a = 1;
  14. start->next = NULL;
  15. s* p, * q;
  16. p = start;
  17. for (int i = 0; i < n - 1; i++) {
  18. q = malloc(sizeof(s));
  19. q->a = p->a + 1;
  20. p->next = q;
  21. p = q;
  22. }
  23. p->next = NULL;
  24. return start;
  25. }
  26. void show(s* list) {
  27. while (list) {
  28. if (list != NULL) {
  29. printf("%d ", list->a);
  30. list = list->next;
  31. }
  32. }
  33. printf("\n");
  34. }
  35. void delete(s* list) {
  36. s* p = list;
  37. while (p->next) {
  38. p = list->next;
  39. free(list);
  40. list = p;
  41. }
  42. }
  43. s* insert(s* list, s item, int k)
  44. {
  45. s* el = malloc(sizeof(s));//âûäåëÿåì ïàìÿòü ïîä íîâûé ýëåìåíò ñïèñêà
  46. el->a = item.a;//ïîìåùàåì â íåãî ïîëå èç ñòðóêòóðû
  47. if (k == 1)//åñëè ìû ìåíÿåì ïåðâûé ýëåìåíò
  48. {
  49. el->next = list;
  50. list = el;
  51. }
  52. else // åñëè ìåíÿåì íå ïåðâûé ýëåìåíò
  53. {
  54. s* start = list;//çàïîìèíàåì ãîëîâó ñïèñêà
  55. for (size_t i = 0; i < k - 1; i++)
  56. {
  57. list = list->next;//ñäâèãàåì íà k ïîçèöèé
  58. if (!(list->next))
  59. {
  60. printf("âû ââåëè èíäåêñ, ïðåâûøàþùèé ðàçìåðû ñïèñêà. ÌÛ âñòàâèì ýëåìåíò â êîíåö ñïèñêà\n");
  61. break;
  62. }
  63. }
  64. void* temp = list->next;//îáìåí àäðåñàìè
  65. list->next = el;
  66. el->next = temp;
  67. list = start;//âîçâðàùàåì ãîëîâó íà ìåñòî
  68. }
  69. return list;
  70. }
  71. void delete_el(s* list) {
  72. printf_s("Ââåäèòå íîìåð ýëåìåíòà ñïèñêà, êîòîðûé âû õîòèòå óäàëèòü\n");
  73. int n = 0;
  74. scanf_s("%d", &n);
  75. if (list == NULL) {
  76. return;
  77. }
  78. s* p = list;
  79. s* prev = NULL;
  80. while (p) {
  81. if (p->a == n) {
  82. if (prev == NULL) {
  83. // Remove head node
  84. list = p->next;
  85. }
  86. else {
  87. prev->next = p->next;
  88. }
  89. free(p);
  90. return;
  91. }
  92. prev = p;
  93. p = p->next;
  94. }
  95. }
  96. void change_element(s*list) {
  97. int c, b = 0;
  98. printf_s("Ââåäèòå ïåðâûé index äëÿ çàìåíû\n");
  99. scanf_s("%d", &c);
  100. printf_s("Ââåäèòå âòîðîé index äëÿ çàìåíû\n");
  101. scanf_s("%d", &b);
  102. if (list == NULL) {
  103. return;
  104. }
  105. s* p = list;
  106. s* prev = NULL;
  107. s* p1 = list;
  108. s* prev1 = NULL;
  109. while (p && p->a != c) {
  110. prev = p;
  111. p = p->next;
  112. }
  113. while (p1 && p1->a != b) {
  114. prev1 = p1;
  115. p1 = p1->next;
  116. }
  117. if (!p || !p1) {
  118. return;
  119. }
  120. if (prev == NULL) {
  121. // p1 is the head node
  122. list = p1;
  123. }
  124. else {
  125. prev->next = p1;
  126. }
  127. if (prev1 == NULL) {
  128. // p2 is the head node
  129. list = p;
  130. }
  131. else {
  132. prev1->next = p;
  133. }
  134. s* temp = p->next;
  135. p->next = p1->next;
  136. p1->next = temp;
  137. }
  138. int main() {
  139. system("chcp 1251>nul");
  140. int siz = 0;
  141. printf_s("Ââåäèòå ðàçìåð ñïèñêà\n");
  142. scanf_s("%d", &siz);
  143. s* list1 = create(siz);
  144. show(list1);
  145. int v = 0;
  146. printf("Âûáåðèòå, ÷òî áóäåì äåëàòü:\n1.Âñòàâèòü çíà÷åíèå.\n2.Óäàëèòü ñïèñîê.\n3.Óäàëèòü ýëåìåíò èç ñïèñêà\n4.Ïîìåíÿòü ìåñòàìè ýëåìåíòû ñïèñêà\n");
  147. scanf_s("%d", &v);
  148. if(v == 1){
  149. int value = 0;
  150. int mesto = 0;
  151. printf_s("Ââåäèòå æåëàåìîå çíà÷åíèå\n");
  152. scanf_s("%d", &value);
  153. s item = { value,NULL };
  154. printf_s("Ââåäèòå ìåñòî, êóäà âû õîòèòå âñòàâèòü çíà÷åíèå\n");
  155. scanf_s("%d", &mesto);
  156. list1 = insert(list1, item, mesto-1);
  157. show(list1);
  158. }
  159. else if (v == 2) {
  160. delete(list1);
  161. show(list1);
  162. }
  163. else if (v == 3) {
  164. delete_el(list1);
  165. show(list1);
  166. }
  167. else if (v == 4) {
  168. change_element(list1);
  169. show(list1);
  170. }
  171. return 0;
  172. }