LineynyeSpiski.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. //Ñòðóêòóðà âåðøèíû ñïèñêà
  6. typedef struct Vertex {
  7. int id; //Ïîðÿäêîâûé íîìåð ñïèñêà
  8. int data; //Äàííûå ýåëìåíòà ñïèñêà
  9. struct Vertex* next; //Óêàçàòåëü íà ñëåäóþùèé ýëåìåíò ñïèñêà
  10. } Vertex;
  11. Vertex* create_node(int id, int data) {
  12. Vertex* new_node = (Vertex*)malloc(sizeof(Vertex));
  13. if (!new_node) {
  14. printf("Îøèáêà âûäåëåíèÿ ïàìÿòè.");
  15. exit(1);
  16. }
  17. new_node->id = id;
  18. new_node->data = data;
  19. new_node->next = NULL;
  20. return new_node;
  21. }
  22. //Ôóíêöèÿ äëÿ äîáàâëåíèÿ ýëåìåíòà â êîíåö ñïèñêà
  23. void add_vertex(Vertex** head, int id, int data) {
  24. Vertex* new_vertex = create_node(id, data);
  25. if (*head == NULL)
  26. {
  27. *head = new_vertex;
  28. }
  29. else {
  30. Vertex* temp = *head;
  31. while (temp->next != NULL) {
  32. temp = temp->next;
  33. }
  34. temp->next = new_vertex;
  35. }
  36. }
  37. //Ôóíêöèÿ äëÿ óäàëåíèÿ âñåãî ñïèñêà
  38. void delete_list(Vertex** head) {
  39. Vertex* current = *head;
  40. Vertex* next = NULL;
  41. while (current != NULL) {
  42. next = current->next;
  43. free(current);
  44. current = next;
  45. }
  46. *head = NULL;
  47. printf("Ñïèñîê óäàëåí \n");
  48. }
  49. //Ôóíêöèÿ äëÿ âñòàâêè íîâîãî ýëåìåíòà â ñïèñîê ïî ïîðÿäêîâîìó íîìåðó
  50. void insert_to_vertex(Vertex** head, int position, int id, int data) {
  51. Vertex* new_vertex = create_node(id, data);
  52. if (position == 0) {
  53. new_vertex->next = *head;
  54. *head = new_vertex;
  55. return;
  56. }
  57. Vertex* temp = *head;
  58. for (int i = 0; temp != NULL && i < position - 1; i++) {
  59. temp = temp->next;
  60. }
  61. if (temp == NULL) {
  62. printf("Ïîçèöèÿ íå íàéäåíà \n");
  63. free(new_vertex);
  64. return;
  65. }
  66. new_vertex->next = temp->next;
  67. temp->next = new_vertex;
  68. }
  69. //Ôóíêöèÿ äëÿ óäàëåíèÿ ýëåìåíòà èç ñïèñêà ïî ïîðÿäêîâîìó íîìåðó
  70. void delete_vertex(Vertex** head, int id) {
  71. Vertex* temp = *head;
  72. Vertex* prev = NULL;
  73. //åñëè óäàëÿåìûé ýëåìåíò - ïåðâûé
  74. if (temp != NULL && temp->id == id) {
  75. *head = temp->next;
  76. free(temp);
  77. return;
  78. }
  79. //Ïîèñê ýëåìåíòà äëÿ óäàëåíèÿ
  80. while (temp != NULL && temp->id != id) {
  81. prev = temp;
  82. temp = temp->next;
  83. }
  84. if (temp == NULL) {
  85. printf("Ýëåìåíåíò ñ id %d íå íàéäåí \n", id);
  86. return;
  87. }
  88. //Óäàëåíèå óçëà
  89. prev->next = temp->next;
  90. free(temp);
  91. }
  92. //Ôóíêöèÿ äëÿ çàìåíû äâóõ ýëåìåíòîâ ìåñòàìè
  93. void swap_vertexes(Vertex** head, int id1, int id2)
  94. {
  95. if (id1 == id2) return; //Åñëè id îäèíàêîâûå, îáìåí íå òðåáóåòñÿ
  96. Vertex* prevX = NULL, * currX = *head;
  97. Vertex* prevY = NULL, * currY = *head;
  98. //Ïîèñê ïåðâîãî ýëåìåíòà
  99. while (currX && currX->id != id1) {
  100. prevX = currX;
  101. currX = currX->next;
  102. }
  103. //Ïîèñê âòîðîãî ýëåìåíòà
  104. while (currY && currY->id != id2) {
  105. prevY = currY;
  106. currY = currY->next;
  107. }
  108. //Åñëè áû õîòÿ áû îäèí ýëåìåíò íå íàéäåí
  109. if (!currX || !currY) {
  110. printf("Îäèí èëè îáà ýëåìåíòà ñ id %d è %d íå íàéäåíû \n", id1, id2);
  111. return;
  112. }
  113. //Åñëè currX íå ÿâëÿåòñÿ ïåðâîé âåðøèíîé
  114. if (prevX != NULL)
  115. prevX->next = currY;
  116. else //åñëè currX - ýòî ïåðâàÿ âåðøèíà
  117. *head = currY;
  118. //Åñëè curryY íå ÿâë ïåðâîé âåðøèíîé
  119. if (prevY != NULL)
  120. prevY->next = currX;
  121. else //Åñëè curryY - ýòî ïåðâàÿ âåðøèíà
  122. *head = currX;
  123. //Îáìåí next óêàçàòåëÿìè
  124. Vertex* temp = currX->next;
  125. currX->next = currY->next;
  126. currY->next = temp;
  127. }
  128. //Ôóíêöèÿ äëÿ îòîáðàæåíèÿ ñïèñêà
  129. void print_list(Vertex* head) {
  130. Vertex* temp = head;
  131. while (temp != NULL) {
  132. printf("ID: %d, Data: %d \n", temp->id, temp->data);
  133. temp = temp->next;
  134. }
  135. }
  136. int main()
  137. {
  138. system("chcp 1251>null");
  139. Vertex* head = NULL;
  140. //Ñîçäàíèå ñïèñêà
  141. add_vertex(&head, 1, 10);
  142. add_vertex(&head, 2, 20);
  143. add_vertex(&head, 3, 30);
  144. add_vertex(&head, 4, 40);
  145. add_vertex(&head, 5, 50);
  146. add_vertex(&head, 6, 60);
  147. add_vertex(&head, 7, 70);
  148. printf("Èñõîäíûé ñïèñîê:\n");
  149. print_list(head);
  150. //Âñòàâêà
  151. printf("\nÂñòàâêà ýëåìåíòà ñ ID 6 íà ïîçèöèþ 0: \n");
  152. insert_to_vertex(&head, 0, 6, 60);
  153. print_list(head);
  154. //Óäàëåíèå
  155. printf("\nÓäàëåíèå ýëåìåíòà ñ ID 0: \n");
  156. delete_vertex(&head, 6);
  157. print_list(head);
  158. //Çàìåíà
  159. printf("\nÇàìåíà ýëåìåíòîâ ñ ID 2 è 4: \n");
  160. swap_vertexes(&head, 1, 2);
  161. print_list(head);
  162. //Óäàëåíèå âñåãî ñïèñêà
  163. delete_list(&head);
  164. return 0;
  165. }