123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include "Header.h"
- /// <summary>
- /// Òî, êàê âûãëÿäèò ýëåìåíò ñïèñêà
- /// </summary>
- struct ElementList
- {
- int value;
- struct ElementList* nextElement;
- };
- typedef struct ElementList ElementList; //îïðåäåëåíèå èìåíè ñòðóêòóðû
- typedef ElementList* List; //îïðåäåëåíèå èìåíè òèïà ëèñòà (ãîëîâû ñïèñêà)
- /// <summary>
- /// Ãåíåðàöèÿ ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.
- /// </summary>
- /// <param name="length">Êîë-âî ýëåìåíòà ñïèñêà.</param>
- /// <returns>Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
- List createIntList(int length)
- {
- if (!length)
- return 1;
- List headOfList = calloc(1, sizeof(ElementList));
- headOfList->value = 1;
- headOfList->nextElement = NULL;
- List currentElement = headOfList, newElement;
- for (int i = 2; i <= length; i++)
- {
- newElement = calloc(1, sizeof(ElementList));
- newElement->value = i;
- currentElement->nextElement = newElement;
- currentElement = newElement;
- }
- currentElement->nextElement = NULL;
- return headOfList;
- }
- /// <summary>
- /// Óäàëåíèå ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.
- /// </summary>
- /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
- void deleteIntList(List list)
- {
- while (list)
- {
- List nextList = list->nextElement;
- free(list);
- list = nextList;
- }
- free(list);
- }
- /// <summary>
- /// Âûâîä ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà íà êîíñîëü.
- /// </summary>
- /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
- void printIntList(List list)
- {
- while (list)
- {
- printf("%d ", list->value);
- list = list->nextElement;
- }
- printf("\n");
- }
- /// <summary>
- /// Äîáàâëåíèå ýåëåìåíòà â ëèíåéíûé îäíîñâÿçíûé ñïèñîê.
- /// </summary>
- /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
- /// <param name="numberElemnt">Ïîçèöèÿ äëÿ âñòàâêè.</param>
- /// <param name="insertedElement">Âñòàâëÿåìûé ýëåìåíò.</param>
- /// <returns>Ãîëîâà èçìåíåííîãî ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
- List insertIntElementInList(List list, int numberElemnt, List insertedElement)
- {
- List headOfList = list;
- if (numberElemnt > 1)
- {
- for (int i = 1; i < numberElemnt - 1; i++)
- {
- list = list->nextElement;
- }
- insertedElement->nextElement = list->nextElement;
- list->nextElement = insertedElement;
- }
- else if (numberElemnt == 1)
- {
- insertedElement->nextElement = list;
- headOfList = insertedElement;
- }
- return headOfList;
- }
- /// <summary>
- /// Óäàëåíèå ýëåìåíòà ëèíåéíîãî îäíîñâÿçííîãî ñïèñêà.
- /// </summary>
- /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
- /// <param name="numberElemnt">Ïîðÿäêîâûé íîìåð óäàëÿåìîãî ýëåìåíòà.</param>
- /// <returns>Ãîëîâà èçìåíåííîãî ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
- List deletIntElementInList(List list, int numberElemnt)
- {
- List headOfList = list;
- if (numberElemnt > 1)
- {
- for (int i = 1; i < numberElemnt - 1; i++)
- {
- list = list->nextElement;
- }
- list->nextElement = list->nextElement->nextElement;
- }
- else if (numberElemnt == 1)
- {
- headOfList = list->nextElement;
- free(list);
- }
- return headOfList;
- }
- /// <summary>
- /// Ñìåíà ýëåìåíòîâ ëèíåéíîãî îäíîñâÿçííîãî ñïèñêà ìåñòàìè.
- /// </summary>
- /// <param name="list">Ãîëîâà ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</param>
- /// <param name="numberFirstElement">Íîìåð ïåðâîãî ýëåìåíòà.</param>
- /// <param name="numberSecondElement">Íîìåð âòîðîãî ýëåìåíòà.</param>
- /// <returns>Ãîëîâà èçìåíåííîãî ëèíåéíîãî îäíîñâÿçíîãî ñïèñêà.</returns>
- List swapElementIntList(List list, int numberFirstElement, int numberSecondElement)
- {
- List headOfList = list;
- //åñëè óêàçàíû îäèíàêîâûå íîìåðà
- if (numberFirstElement == numberSecondElement)
- {
- return headOfList;
- }
- //ñìåíà íîìåðîâ ìåñòàìè, åñëè ïåðâûì óêàçàí áîëüøèé
- if (numberFirstElement > numberSecondElement)
- {
- int temp = numberFirstElement;
- numberFirstElement = numberSecondElement;
- numberSecondElement = temp;
- }
- //åñëè íóæíî ïîìåíÿòü ãîëîâó ñïèñêà
- if (numberFirstElement == 1)
- {
- for (int i = 1; i < numberSecondElement - 1; i++)
- {
- list = list->nextElement;
- }
- List currentElement = list->nextElement;
- List nextAfterCurrentElement = currentElement->nextElement;
- currentElement->nextElement = headOfList->nextElement;
- list->nextElement = headOfList;
- headOfList->nextElement = nextAfterCurrentElement;
- return currentElement;
- }
- //èíûå ñëó÷àè swap'à
- else
- {
- for (int i = 1; i < numberFirstElement - 1; i++)
- {
- list = list->nextElement;
- }
- List prefFirstCurrentElement = list; //Ýëåìåíò ïåðåä ïåðâûì öåëåâûì ýëåìåíòîì
- for (int i = numberFirstElement - 1; i < numberSecondElement - 1; i++)
- {
- list = list->nextElement;
- }
- List prefSecondCurrentElement = list; //Ýëåìåíò ïåðåä âòîðûì öåëåâûì ýëåìåíòîì
- List nextFirstCurrentElemnt = prefFirstCurrentElement->nextElement->nextElement; //Ñîõðàíåíèå àäðåñà-ññûëêè ïåðâîãî ýëåìåíòà äëÿ îáìåíà
- prefFirstCurrentElement->nextElement->nextElement = prefSecondCurrentElement->nextElement->nextElement;
- prefSecondCurrentElement->nextElement->nextElement = nextFirstCurrentElemnt;
- List firstCurrentElemnt = prefFirstCurrentElement->nextElement; //Ñîõðàíåíèå àäðåñà-ññûëêè âòîðîãî ýëåìåíòà äëÿ îáìåíà
- prefFirstCurrentElement->nextElement = prefSecondCurrentElement->nextElement;
- prefSecondCurrentElement->nextElement = firstCurrentElemnt;
- return headOfList;
- }
- }
- int startWorkWithLinelist()
- {
-
- //Ñîçäàíèå ëèñòà
- List MyList = createIntList(10);
- printIntList(MyList);
- //Äîáàâëåíèå ýëåìåíòà
- List newElemnt = calloc(1, sizeof(ElementList));
- newElemnt->value = 99;
- MyList = insertIntElementInList(MyList, 1, newElemnt);
- printIntList(MyList);
- //Óäàëåíèå ýäåìåíòà
- MyList = deletIntElementInList(MyList, 1);
- printIntList(MyList);
- //Ïîìåíÿòü ýëåìåíòû ìåñòàìè
- MyList = swapElementIntList(MyList, 10, 1);
- printIntList(MyList);
- //Óäàëåíèå ñïèñêà
- deleteIntList(MyList);
- return 0;
- }
|