lineList.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "Header.h"
  2. /// <summary>
  3. /// Òî, êàê âûãëÿäèò ýëåìåíò ñïèñêà
  4. /// </summary>
  5. struct ElementList
  6. {
  7. int value;
  8. struct ElementList* nextElement;
  9. };
  10. typedef struct ElementList ElementList; //îïðåäåëåíèå èìåíè ñòðóêòóðû
  11. typedef ElementList* List; //îïðåäåëåíèå èìåíè òèïà ëèñòà (ãîëîâû ñïèñêà)
  12. /// <summary>
  13. /// Ãåíåðàöèÿ ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.
  14. /// </summary>
  15. /// <param name="length">Êîë-âî ýëåìåíòà ñïèñêà.</param>
  16. /// <returns>Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
  17. List createIntList(int length)
  18. {
  19. if (!length)
  20. return 1;
  21. List headOfList = calloc(1, sizeof(ElementList));
  22. headOfList->value = 1;
  23. headOfList->nextElement = NULL;
  24. List currentElement = headOfList, newElement;
  25. for (int i = 2; i <= length; i++)
  26. {
  27. newElement = calloc(1, sizeof(ElementList));
  28. newElement->value = i;
  29. currentElement->nextElement = newElement;
  30. currentElement = newElement;
  31. }
  32. currentElement->nextElement = NULL;
  33. return headOfList;
  34. }
  35. /// <summary>
  36. /// Óäàëåíèå ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.
  37. /// </summary>
  38. /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
  39. void deleteIntList(List list)
  40. {
  41. while (list)
  42. {
  43. List nextList = list->nextElement;
  44. free(list);
  45. list = nextList;
  46. }
  47. free(list);
  48. }
  49. /// <summary>
  50. /// Âûâîä ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà íà êîíñîëü.
  51. /// </summary>
  52. /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
  53. void printIntList(List list)
  54. {
  55. while (list)
  56. {
  57. printf("%d ", list->value);
  58. list = list->nextElement;
  59. }
  60. printf("\n");
  61. }
  62. /// <summary>
  63. /// Äîáàâëåíèå ýåëåìåíòà â ëèíåéíûé îäíîñâÿçíûé ñïèñîê.
  64. /// </summary>
  65. /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
  66. /// <param name="numberElemnt">Ïîçèöèÿ äëÿ âñòàâêè.</param>
  67. /// <param name="insertedElement">Âñòàâëÿåìûé ýëåìåíò.</param>
  68. /// <returns>Ãîëîâà èçìåíåííîãî ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
  69. List insertIntElementInList(List list, int numberElemnt, List insertedElement)
  70. {
  71. List headOfList = list;
  72. if (numberElemnt > 1)
  73. {
  74. for (int i = 1; i < numberElemnt - 1; i++)
  75. {
  76. list = list->nextElement;
  77. }
  78. insertedElement->nextElement = list->nextElement;
  79. list->nextElement = insertedElement;
  80. }
  81. else if (numberElemnt == 1)
  82. {
  83. insertedElement->nextElement = list;
  84. headOfList = insertedElement;
  85. }
  86. return headOfList;
  87. }
  88. /// <summary>
  89. /// Óäàëåíèå ýëåìåíòà ëèíåéíîãî îäíîñâÿçííîãî ñïèñêà.
  90. /// </summary>
  91. /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
  92. /// <param name="numberElemnt">Ïîðÿäêîâûé íîìåð óäàëÿåìîãî ýëåìåíòà.</param>
  93. /// <returns>Ãîëîâà èçìåíåííîãî ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
  94. List deletIntElementInList(List list, int numberElemnt)
  95. {
  96. List headOfList = list;
  97. if (numberElemnt > 1)
  98. {
  99. for (int i = 1; i < numberElemnt - 1; i++)
  100. {
  101. list = list->nextElement;
  102. }
  103. list->nextElement = list->nextElement->nextElement;
  104. }
  105. else if (numberElemnt == 1)
  106. {
  107. headOfList = list->nextElement;
  108. free(list);
  109. }
  110. return headOfList;
  111. }
  112. /// <summary>
  113. /// Ñìåíà ýëåìåíòîâ ëèíåéíîãî îäíîñâÿçííîãî ñïèñêà ìåñòàìè.
  114. /// </summary>
  115. /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
  116. /// <param name="numberFirstElement">Íîìåð ïåðâîãî ýëåìåíòà.</param>
  117. /// <param name="numberSecondElement">Íîìåð âòîðîãî ýëåìåíòà.</param>
  118. /// <returns>Ãîëîâà èçìåíåííîãî ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
  119. List swapElementIntList(List list, int numberFirstElement, int numberSecondElement)
  120. {
  121. List headOfList = list;
  122. //åñëè óêàçàíû îäèíàêîâûå íîìåðà
  123. if (numberFirstElement == numberSecondElement)
  124. {
  125. return headOfList;
  126. }
  127. //ñìåíà íîìåðîâ ìåñòàìè, åñëè ïåðâûì óêàçàí áîëüøèé
  128. if (numberFirstElement > numberSecondElement)
  129. {
  130. int temp = numberFirstElement;
  131. numberFirstElement = numberSecondElement;
  132. numberSecondElement = temp;
  133. }
  134. //åñëè íóæíî ïîìåíÿòü ãîëîâó ñïèñêà
  135. if (numberFirstElement == 1)
  136. {
  137. for (int i = 1; i < numberSecondElement - 1; i++)
  138. {
  139. list = list->nextElement;
  140. }
  141. List currentElement = list->nextElement;
  142. List nextAfterCurrentElement = currentElement->nextElement;
  143. currentElement->nextElement = headOfList->nextElement;
  144. list->nextElement = headOfList;
  145. headOfList->nextElement = nextAfterCurrentElement;
  146. return currentElement;
  147. }
  148. //èíûå ñëó÷àè swap'à
  149. else
  150. {
  151. for (int i = 1; i < numberFirstElement - 1; i++)
  152. {
  153. list = list->nextElement;
  154. }
  155. List prefFirstCurrentElement = list; //Ýëåìåíò ïåðåä ïåðâûì öåëåâûì ýëåìåíòîì
  156. for (int i = numberFirstElement - 1; i < numberSecondElement - 1; i++)
  157. {
  158. list = list->nextElement;
  159. }
  160. List prefSecondCurrentElement = list; //Ýëåìåíò ïåðåä âòîðûì öåëåâûì ýëåìåíòîì
  161. List nextFirstCurrentElemnt = prefFirstCurrentElement->nextElement->nextElement; //Ñîõðàíåíèå àäðåñà-ññûëêè ïåðâîãî ýëåìåíòà äëÿ îáìåíà
  162. prefFirstCurrentElement->nextElement->nextElement = prefSecondCurrentElement->nextElement->nextElement;
  163. prefSecondCurrentElement->nextElement->nextElement = nextFirstCurrentElemnt;
  164. List firstCurrentElemnt = prefFirstCurrentElement->nextElement; //Ñîõðàíåíèå àäðåñà-ññûëêè âòîðîãî ýëåìåíòà äëÿ îáìåíà
  165. prefFirstCurrentElement->nextElement = prefSecondCurrentElement->nextElement;
  166. prefSecondCurrentElement->nextElement = firstCurrentElemnt;
  167. return headOfList;
  168. }
  169. }
  170. int startWorkWithLinelist()
  171. {
  172. //Ñîçäàíèå ëèñòà
  173. List MyList = createIntList(10);
  174. printIntList(MyList);
  175. //Äîáàâëåíèå ýëåìåíòà
  176. List newElemnt = calloc(1, sizeof(ElementList));
  177. newElemnt->value = 99;
  178. MyList = insertIntElementInList(MyList, 1, newElemnt);
  179. printIntList(MyList);
  180. //Óäàëåíèå ýäåìåíòà
  181. MyList = deletIntElementInList(MyList, 1);
  182. printIntList(MyList);
  183. //Ïîìåíÿòü ýëåìåíòû ìåñòàìè
  184. MyList = swapElementIntList(MyList, 10, 1);
  185. printIntList(MyList);
  186. //Óäàëåíèå ñïèñêà
  187. deleteIntList(MyList);
  188. return 0;
  189. }