From e700253c13a48d4d29d86b8e2984e661016d1cb4 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 30 Oct 2021 23:22:17 +0500 Subject: [PATCH 01/16] Writing functions for a cyclic list and texts to them, solving the problem --- CyclicList/CyclicList.sln | 31 ++++ CyclicList/CyclicList/CircleOfMurders.c | 55 ++++++ CyclicList/CyclicList/CircleOfMurders.h | 4 + CyclicList/CyclicList/CyclicList.c | 113 +++++++++++++ CyclicList/CyclicList/CyclicList.h | 45 +++++ CyclicList/CyclicList/CyclicList.vcxproj | 157 ++++++++++++++++++ .../CyclicList/CyclicList.vcxproj.filters | 48 ++++++ CyclicList/CyclicList/Main.c | 40 +++++ CyclicList/CyclicList/TestCyclicList.c | 51 ++++++ CyclicList/CyclicList/TestCyclicList.h | 5 + .../CyclicList/TestFindSurvivorPosition.c | 9 + .../CyclicList/TestFindSurvivorPosition.h | 5 + 12 files changed, 563 insertions(+) create mode 100644 CyclicList/CyclicList.sln create mode 100644 CyclicList/CyclicList/CircleOfMurders.c create mode 100644 CyclicList/CyclicList/CircleOfMurders.h create mode 100644 CyclicList/CyclicList/CyclicList.c create mode 100644 CyclicList/CyclicList/CyclicList.h create mode 100644 CyclicList/CyclicList/CyclicList.vcxproj create mode 100644 CyclicList/CyclicList/CyclicList.vcxproj.filters create mode 100644 CyclicList/CyclicList/Main.c create mode 100644 CyclicList/CyclicList/TestCyclicList.c create mode 100644 CyclicList/CyclicList/TestCyclicList.h create mode 100644 CyclicList/CyclicList/TestFindSurvivorPosition.c create mode 100644 CyclicList/CyclicList/TestFindSurvivorPosition.h diff --git a/CyclicList/CyclicList.sln b/CyclicList/CyclicList.sln new file mode 100644 index 0000000..b7d7dd6 --- /dev/null +++ b/CyclicList/CyclicList.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31410.357 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CyclicList", "CyclicList\CyclicList.vcxproj", "{3E224E7A-155B-41F9-8988-80A502835B6E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.ActiveCfg = Debug|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x64.Build.0 = Debug|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.ActiveCfg = Debug|Win32 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Debug|x86.Build.0 = Debug|Win32 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.ActiveCfg = Release|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x64.Build.0 = Release|x64 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.ActiveCfg = Release|Win32 + {3E224E7A-155B-41F9-8988-80A502835B6E}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {11CCDD59-DDF5-48B1-85A1-8BE50AF8E546} + EndGlobalSection +EndGlobal diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c new file mode 100644 index 0000000..81f0855 --- /dev/null +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -0,0 +1,55 @@ +#include "CircleOfMurders.h" +#include "CyclicList.h" + +int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error) +{ + if (numberOfWarriors == 0) + { + *error = 2; + return 0; + } + if (frequencyOfMurders == 0) + { + *error = 2; + return 0; + } + List* newList = createList(); + int errorCode = 0; + for (int i = 1; i <= numberOfWarriors; i++) + { + add(newList, i, &errorCode); + if (errorCode == 3) + { + deleteList(newList); + *error = 3; + return 0; + } + } + Position* firstPosition = first(newList, &errorCode); + if (errorCode == 3) + { + deleteList(newList); + *error = 3; + return 0; + } + int counter = 1; + while (newList->head != newList->tail) + { + if (counter % frequencyOfMurders == 0) + { + removeElement(newList, firstPosition, &errorCode); + if (errorCode == 1) + { + deleteList(newList); + *error = 1; + return 0; + } + previous(firstPosition); + } + next(firstPosition); + counter++; + } + const int answer = newList->head->value; + deleteList(newList); + return answer; +} \ No newline at end of file diff --git a/CyclicList/CyclicList/CircleOfMurders.h b/CyclicList/CyclicList/CircleOfMurders.h new file mode 100644 index 0000000..2877238 --- /dev/null +++ b/CyclicList/CyclicList/CircleOfMurders.h @@ -0,0 +1,4 @@ +#pragma once + +// Function for finding the position of the surviving warrior +int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error); \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c new file mode 100644 index 0000000..cba9676 --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.c @@ -0,0 +1,113 @@ +#include "CyclicList.h" +#include + +List* createList() +{ + return calloc(1, sizeof(List)); +} + +void deleteList(List* list) +{ + ListElement* position = list->head; + while (list->head != list->tail) + { + list->head = list->head->next; + free(position); + position = list->head; + } + free(position); + free(list); +} + +Position* first(List* list, int* error) +{ + *error = 0; + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = 3; + return NULL; + } + positionFirst->position = list->head; + if (positionFirst->position == NULL) + { + return NULL; + } + positionFirst->position->previous = list->tail; + return positionFirst; +} + +Position* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +Position* previous(Position* position) +{ + position->position = position->position->previous; + return position; +} + +void add(List* list, int value, int* error) +{ + int errorCode = 0; + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + *error = 3; + return; + } + Position* firstPosition = first(list, &errorCode); + if (errorCode == 3) + { + *error = 3; + return; + } + newElement->value = value; + if (firstPosition->position == NULL) + { + list->head = newElement; + list->tail = newElement; + list->tail->next = list->head; + list->head->next = list->tail; + return; + } + newElement->previous = list->tail; + list->tail->next = newElement; + newElement->next = list->head; + list->tail = newElement; +} + +void removeElement(List* list, Position* position, int* error) +{ + if (list->head == NULL || list->tail == NULL) + { + *error = 1; + return; + } + if (position->position == list->head) + { + if (list->tail == list->head) + { + list->tail = NULL; + list->head = NULL; + return; + } + list->tail->next = list->head->next; + list->head->next->previous = list->tail; + list->head = list->head->next; + return; + } + if (position->position == list->tail) + { + list->tail->previous->next = list->head; + list->head->previous = list->tail->previous; + list->tail = list->tail->previous; + return; + } + position->position->previous->next = position->position->next; + position->position->next->previous = position->position->previous; + position->position->previous = position->position; + position->position = position->position->next; +} \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.h b/CyclicList/CyclicList/CyclicList.h new file mode 100644 index 0000000..8046915 --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.h @@ -0,0 +1,45 @@ +#pragma once +#include +#include + +// Structure containing pointers to the "first" and "last" list item +typedef struct List +{ + struct ListElement* head; + struct ListElement* tail; +} List; + +// Structure containing pointers to the next and previous list item and a value variable for list items +typedef struct ListElement +{ + int value; + struct ListElement* next; + struct ListElement* previous; +} ListElement; + +// Structure containing a pointer to the position of a list item +typedef struct Position +{ + ListElement* position; +} Position; + +// Function for creating a list +List* createList(); + +// Function for deleting a list +void deleteList(List* list); + +// Function for adding an item to a list +void add(List* list, int value, int* error); + +// Function for the first position +Position* first(List* list, int* error); + +// Function to remove an item from the list +void removeElement(List* list, Position* position, int* error); + +// Function for moving to the next position +Position* next(Position* position); + +// Function for moving to the previous position +Position* previous(Position* position); \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.vcxproj b/CyclicList/CyclicList/CyclicList.vcxproj new file mode 100644 index 0000000..284596f --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.vcxproj @@ -0,0 +1,157 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {3e224e7a-155b-41f9-8988-80a502835b6e} + CyclicList + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.vcxproj.filters b/CyclicList/CyclicList/CyclicList.vcxproj.filters new file mode 100644 index 0000000..639547e --- /dev/null +++ b/CyclicList/CyclicList/CyclicList.vcxproj.filters @@ -0,0 +1,48 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/CyclicList/CyclicList/Main.c b/CyclicList/CyclicList/Main.c new file mode 100644 index 0000000..1acc1a2 --- /dev/null +++ b/CyclicList/CyclicList/Main.c @@ -0,0 +1,40 @@ +#include "CyclicList.h" +#include "TestCyclicList.h" +#include "CircleOfMurders.h" +#include "TestFindSurvivorPosition.h" +#include +#include + +int main() +{ + setlocale(LC_ALL, "rus"); + if (!allTest() || !testFindSurviviorPosition()) + { + printf(" "); + return -1; + } + printf(" \n"); + int numberOfWarriors = 0; + scanf_s("%d", &numberOfWarriors); + printf(" m- . m\n"); + int frequencyOfMurders = 0; + scanf_s("%d", &frequencyOfMurders); + int error = 0; + const int answer = findSurvivorPosition(numberOfWarriors, frequencyOfMurders, &error); + if (error == 1) + { + printf(" "); + return -1; + } + if (error == 2) + { + printf(" :)"); + return 0; + } + if (error == 3) + { + printf(" "); + return -1; + } + printf(" - %d", answer); +} \ No newline at end of file diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c new file mode 100644 index 0000000..e739c04 --- /dev/null +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -0,0 +1,51 @@ +#include "CyclicList.h" +#include "TestCyclicList.h" + +// Function to check the function that adds an item to the list +bool testAdd() +{ + int error = 0; + List* newList = createList(); + add(newList, 1, &error); + add(newList, 123, &error); + add(newList, 34, &error); + if (error == 3) + { + return false; + } + const int firstNumber = newList->head->value; + const int secondNumber = newList->head->next->value; + const int thirdNumber = newList->head->next->next->value; + deleteList(newList); + return firstNumber == 1 && secondNumber == 123 && thirdNumber == 34; +} + +// Function for checking a function that deletes a list item +bool testRemoveElement() +{ + int error = 0; + List* newList = createList(); + add(newList, 10, &error); + add(newList, 20, &error); + add(newList, 30, &error); + if (error == 3) + { + return false; + } + Position* position = first(newList, &error); + next(position); + removeElement(newList, position, &error); + if (error == 1) + { + return false; + } + const int firstNumber = newList->head->value; + const int secondNumber = newList->head->next->value; + deleteList(newList); + return firstNumber == 10 && secondNumber == 30; +} + +bool allTest() +{ + return testAdd() && testRemoveElement(); +} \ No newline at end of file diff --git a/CyclicList/CyclicList/TestCyclicList.h b/CyclicList/CyclicList/TestCyclicList.h new file mode 100644 index 0000000..ca937d7 --- /dev/null +++ b/CyclicList/CyclicList/TestCyclicList.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function that combines the results of all tests +bool allTest(); \ No newline at end of file diff --git a/CyclicList/CyclicList/TestFindSurvivorPosition.c b/CyclicList/CyclicList/TestFindSurvivorPosition.c new file mode 100644 index 0000000..1217ac2 --- /dev/null +++ b/CyclicList/CyclicList/TestFindSurvivorPosition.c @@ -0,0 +1,9 @@ +#include "TestFindSurvivorPosition.h" +#include "CircleOfMurders.h" + +bool testFindSurviviorPosition() +{ + int error = 0; + return findSurvivorPosition(10 , 5, &error) == 3 && findSurvivorPosition(7, 4, &error) == 2 + && findSurvivorPosition(20, 3, &error) == 20 && findSurvivorPosition(15, 7, &error) == 5 && findSurvivorPosition(12, 3, &error) == 10;; +} \ No newline at end of file diff --git a/CyclicList/CyclicList/TestFindSurvivorPosition.h b/CyclicList/CyclicList/TestFindSurvivorPosition.h new file mode 100644 index 0000000..f608340 --- /dev/null +++ b/CyclicList/CyclicList/TestFindSurvivorPosition.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function for testing a function that finds the position of a surviving warrior +bool testFindSurviviorPosition(); \ No newline at end of file From 06ddbf7a9adac55eaad85d90cce32d5b4e682b2f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 17:25:03 +0500 Subject: [PATCH 02/16] fixed memory bugs --- CyclicList/CyclicList/TestCyclicList.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index e739c04..c05947a 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -11,6 +11,7 @@ bool testAdd() add(newList, 34, &error); if (error == 3) { + deleteList(newList); return false; } const int firstNumber = newList->head->value; @@ -30,6 +31,7 @@ bool testRemoveElement() add(newList, 30, &error); if (error == 3) { + deleteList(newList); return false; } Position* position = first(newList, &error); @@ -37,6 +39,7 @@ bool testRemoveElement() removeElement(newList, position, &error); if (error == 1) { + deleteList(newList); return false; } const int firstNumber = newList->head->value; From e6d1b29660edec691c98e51acf326bcfc57affd2 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 21:40:00 +0500 Subject: [PATCH 03/16] find error --- CyclicList/CyclicList/TestCyclicList.c | 2 +- CyclicList/CyclicList/TestFindSurvivorPosition.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index c05947a..8cdbfce 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -50,5 +50,5 @@ bool testRemoveElement() bool allTest() { - return testAdd() && testRemoveElement(); + return testRemoveElement(); } \ No newline at end of file diff --git a/CyclicList/CyclicList/TestFindSurvivorPosition.c b/CyclicList/CyclicList/TestFindSurvivorPosition.c index 1217ac2..0841312 100644 --- a/CyclicList/CyclicList/TestFindSurvivorPosition.c +++ b/CyclicList/CyclicList/TestFindSurvivorPosition.c @@ -5,5 +5,5 @@ bool testFindSurviviorPosition() { int error = 0; return findSurvivorPosition(10 , 5, &error) == 3 && findSurvivorPosition(7, 4, &error) == 2 - && findSurvivorPosition(20, 3, &error) == 20 && findSurvivorPosition(15, 7, &error) == 5 && findSurvivorPosition(12, 3, &error) == 10;; + && findSurvivorPosition(20, 3, &error) == 20 && findSurvivorPosition(15, 7, &error) == 5 && findSurvivorPosition(12, 3, &error) == 10; } \ No newline at end of file From f3e15123def603853c0226154927150a6215f59d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 22:11:50 +0500 Subject: [PATCH 04/16] the program is made working --- CyclicList/CyclicList/CyclicList.c | 11 +++++------ CyclicList/CyclicList/Main.c | 2 +- CyclicList/CyclicList/TestCyclicList.c | 2 +- CyclicList/CyclicList/TestFindSurvivorPosition.c | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index cba9676..0dee24e 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -29,16 +29,15 @@ Position* first(List* list, int* error) return NULL; } positionFirst->position = list->head; - if (positionFirst->position == NULL) - { - return NULL; - } - positionFirst->position->previous = list->tail; return positionFirst; } Position* next(Position* position) { + if (position->position == NULL) + { + return NULL; + } position->position = position->position->next; return position; } @@ -81,7 +80,7 @@ void add(List* list, int value, int* error) void removeElement(List* list, Position* position, int* error) { - if (list->head == NULL || list->tail == NULL) + if (list->head == NULL || list->tail == NULL || position->position == NULL) { *error = 1; return; diff --git a/CyclicList/CyclicList/Main.c b/CyclicList/CyclicList/Main.c index 1acc1a2..1333299 100644 --- a/CyclicList/CyclicList/Main.c +++ b/CyclicList/CyclicList/Main.c @@ -8,7 +8,7 @@ int main() { setlocale(LC_ALL, "rus"); - if (!allTest() || !testFindSurviviorPosition()) + if (!allTest() ) { printf(" "); return -1; diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index 8cdbfce..94e1396 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -50,5 +50,5 @@ bool testRemoveElement() bool allTest() { - return testRemoveElement(); + return testRemoveElement() && testAdd(); } \ No newline at end of file diff --git a/CyclicList/CyclicList/TestFindSurvivorPosition.c b/CyclicList/CyclicList/TestFindSurvivorPosition.c index 0841312..3bf0f9f 100644 --- a/CyclicList/CyclicList/TestFindSurvivorPosition.c +++ b/CyclicList/CyclicList/TestFindSurvivorPosition.c @@ -4,6 +4,6 @@ bool testFindSurviviorPosition() { int error = 0; - return findSurvivorPosition(10 , 5, &error) == 3 && findSurvivorPosition(7, 4, &error) == 2 + return findSurvivorPosition(10 , 5, &error) == 3 && findSurvivorPosition(7, 4, &error) == 3 && findSurvivorPosition(20, 3, &error) == 20 && findSurvivorPosition(15, 7, &error) == 5 && findSurvivorPosition(12, 3, &error) == 10; } \ No newline at end of file From 5392d0c94eb9da77c4182564dbb9934994293df8 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 22:38:10 +0500 Subject: [PATCH 05/16] Now definitely a worker --- CyclicList/CyclicList/CyclicList.c | 8 ++------ CyclicList/CyclicList/Main.c | 2 +- CyclicList/CyclicList/TestFindSurvivorPosition.c | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index 0dee24e..2e68759 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -34,10 +34,6 @@ Position* first(List* list, int* error) Position* next(Position* position) { - if (position->position == NULL) - { - return NULL; - } position->position = position->position->next; return position; } @@ -68,7 +64,7 @@ void add(List* list, int value, int* error) { list->head = newElement; list->tail = newElement; - list->tail->next = list->head; + list->tail->previous = list->head; list->head->next = list->tail; return; } @@ -80,7 +76,7 @@ void add(List* list, int value, int* error) void removeElement(List* list, Position* position, int* error) { - if (list->head == NULL || list->tail == NULL || position->position == NULL) + if (list->head == NULL || list->tail == NULL) { *error = 1; return; diff --git a/CyclicList/CyclicList/Main.c b/CyclicList/CyclicList/Main.c index 1333299..1acc1a2 100644 --- a/CyclicList/CyclicList/Main.c +++ b/CyclicList/CyclicList/Main.c @@ -8,7 +8,7 @@ int main() { setlocale(LC_ALL, "rus"); - if (!allTest() ) + if (!allTest() || !testFindSurviviorPosition()) { printf(" "); return -1; diff --git a/CyclicList/CyclicList/TestFindSurvivorPosition.c b/CyclicList/CyclicList/TestFindSurvivorPosition.c index 3bf0f9f..0f05c23 100644 --- a/CyclicList/CyclicList/TestFindSurvivorPosition.c +++ b/CyclicList/CyclicList/TestFindSurvivorPosition.c @@ -4,6 +4,6 @@ bool testFindSurviviorPosition() { int error = 0; - return findSurvivorPosition(10 , 5, &error) == 3 && findSurvivorPosition(7, 4, &error) == 3 + return findSurvivorPosition(10, 5, &error) == 3 && findSurvivorPosition(7, 4, &error) == 2 && findSurvivorPosition(20, 3, &error) == 20 && findSurvivorPosition(15, 7, &error) == 5 && findSurvivorPosition(12, 3, &error) == 10; } \ No newline at end of file From 400deae96d5bc5b46398d8d044a78612a78c551a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 22:53:01 +0500 Subject: [PATCH 06/16] fixed memory leaks --- CyclicList/CyclicList/CircleOfMurders.c | 2 ++ CyclicList/CyclicList/CyclicList.c | 11 +++-------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c index 81f0855..79afc2e 100644 --- a/CyclicList/CyclicList/CircleOfMurders.c +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -40,6 +40,7 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro removeElement(newList, firstPosition, &errorCode); if (errorCode == 1) { + free(firstPosition); deleteList(newList); *error = 1; return 0; @@ -50,6 +51,7 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro counter++; } const int answer = newList->head->value; + free(firstPosition); deleteList(newList); return answer; } \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index 2e68759..7519b47 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -53,17 +53,12 @@ void add(List* list, int value, int* error) *error = 3; return; } - Position* firstPosition = first(list, &errorCode); - if (errorCode == 3) - { - *error = 3; - return; - } newElement->value = value; - if (firstPosition->position == NULL) + if (list->head == NULL) { list->head = newElement; list->tail = newElement; + list->tail->next = list->head; list->tail->previous = list->head; list->head->next = list->tail; return; @@ -71,7 +66,7 @@ void add(List* list, int value, int* error) newElement->previous = list->tail; list->tail->next = newElement; newElement->next = list->head; - list->tail = newElement; + list->tail = list->tail->next; } void removeElement(List* list, Position* position, int* error) From abcb64b31a2c72da7ff58be9f6b1661777528802 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 23:20:55 +0500 Subject: [PATCH 07/16] fixed memory leaks --- CyclicList/CyclicList/CircleOfMurders.c | 1 - CyclicList/CyclicList/TestCyclicList.c | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c index 79afc2e..95919b9 100644 --- a/CyclicList/CyclicList/CircleOfMurders.c +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -51,7 +51,6 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro counter++; } const int answer = newList->head->value; - free(firstPosition); deleteList(newList); return answer; } \ No newline at end of file diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index 94e1396..412b421 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -44,6 +44,7 @@ bool testRemoveElement() } const int firstNumber = newList->head->value; const int secondNumber = newList->head->next->value; + free(position); deleteList(newList); return firstNumber == 10 && secondNumber == 30; } From 6f2d365052b1d40423438b3c628d7b983d8dc6cb Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 5 Nov 2021 18:52:35 +0500 Subject: [PATCH 08/16] started fixing tests --- CyclicList/CyclicList/CircleOfMurders.c | 6 +- CyclicList/CyclicList/CyclicList.c | 84 ++++++++++++++++++++----- CyclicList/CyclicList/CyclicList.h | 30 ++++----- CyclicList/CyclicList/Main.c | 2 +- CyclicList/CyclicList/TestCyclicList.c | 18 +++--- 5 files changed, 97 insertions(+), 43 deletions(-) diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c index 95919b9..5f9e764 100644 --- a/CyclicList/CyclicList/CircleOfMurders.c +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -33,11 +33,11 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro return 0; } int counter = 1; - while (newList->head != newList->tail) + while (returnFirstElement(newList) != returnLastElement(newList)) { if (counter % frequencyOfMurders == 0) { - removeElement(newList, firstPosition, &errorCode); + removeElement(newList, &firstPosition, &errorCode); if (errorCode == 1) { free(firstPosition); @@ -50,7 +50,7 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro next(firstPosition); counter++; } - const int answer = newList->head->value; + const int answer = returnFirstElementValue(newList); deleteList(newList); return answer; } \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index 7519b47..1b8884a 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -1,6 +1,27 @@ #include "CyclicList.h" #include +// Structure containing pointers to the "first" and "last" list item +typedef struct List +{ + struct ListElement* head; + struct ListElement* tail; +} List; + +// Structure containing pointers to the next and previous list item and a value variable for list items +typedef struct ListElement +{ + int value; + struct ListElement* next; + struct ListElement* previous; +} ListElement; + +// Structure containing a pointer to the position of a list item +typedef struct Position +{ + ListElement* position; +} Position; + List* createList() { return calloc(1, sizeof(List)); @@ -44,6 +65,12 @@ Position* previous(Position* position) return position; } +void invariant(ListElement* element) +{ + element->next->previous = element; + element->previous->next = element; +} + void add(List* list, int value, int* error) { int errorCode = 0; @@ -59,45 +86,72 @@ void add(List* list, int value, int* error) list->head = newElement; list->tail = newElement; list->tail->next = list->head; - list->tail->previous = list->head; - list->head->next = list->tail; + list->head->previous = list->tail; return; } - newElement->previous = list->tail; - list->tail->next = newElement; newElement->next = list->head; + newElement->previous = list->tail; + invariant(newElement); list->tail = list->tail->next; } -void removeElement(List* list, Position* position, int* error) +int returnFirstElementValue(List* list) +{ + return list->head->value; +} + +void removeElement(List* list, Position** position, int* error) { if (list->head == NULL || list->tail == NULL) { *error = 1; return; } - if (position->position == list->head) + if ((*position)->position == list->head) { if (list->tail == list->head) { list->tail = NULL; list->head = NULL; + free(list->head); return; } - list->tail->next = list->head->next; - list->head->next->previous = list->tail; + ListElement* element = list->head; list->head = list->head->next; + list->head->previous = list->tail; + invariant(list->head); + (*position)->position = list->head; + free(element); return; } - if (position->position == list->tail) + if ((*position)->position == list->tail) { - list->tail->previous->next = list->head; - list->head->previous = list->tail->previous; + ListElement* element = list->tail; list->tail = list->tail->previous; + list->tail->next = list->head; + invariant(list->tail); + (*position)->position = list->head; + free(element); return; } - position->position->previous->next = position->position->next; - position->position->next->previous = position->position->previous; - position->position->previous = position->position; - position->position = position->position->next; + ListElement* element = (*position)->position; + (*position)->position = (*position)->position->next; + (*position)->position->previous = (*position)->position->previous->previous; + free(element); + invariant((*position)->position); +} + +int get(Position* position) +{ + return position->position->value; +} + +ListElement* returnFirstElement(List* list) +{ + return list->head; +} + +ListElement* returnLastElement(List* list) +{ + return list->tail; } \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.h b/CyclicList/CyclicList/CyclicList.h index 8046915..1a036d6 100644 --- a/CyclicList/CyclicList/CyclicList.h +++ b/CyclicList/CyclicList/CyclicList.h @@ -3,25 +3,13 @@ #include // Structure containing pointers to the "first" and "last" list item -typedef struct List -{ - struct ListElement* head; - struct ListElement* tail; -} List; +typedef struct List List; // Structure containing pointers to the next and previous list item and a value variable for list items -typedef struct ListElement -{ - int value; - struct ListElement* next; - struct ListElement* previous; -} ListElement; +typedef struct ListElement ListElement; // Structure containing a pointer to the position of a list item -typedef struct Position -{ - ListElement* position; -} Position; +typedef struct Position Position; // Function for creating a list List* createList(); @@ -36,10 +24,18 @@ void add(List* list, int value, int* error); Position* first(List* list, int* error); // Function to remove an item from the list -void removeElement(List* list, Position* position, int* error); +void removeElement(List* list, Position** position, int* error); // Function for moving to the next position Position* next(Position* position); // Function for moving to the previous position -Position* previous(Position* position); \ No newline at end of file +Position* previous(Position* position); + +int returnFirstElementValue(List* list); + +int get(Position* position); + +ListElement* returnFirstElement(List* list); + +ListElement* returnLastElement(List* list); diff --git a/CyclicList/CyclicList/Main.c b/CyclicList/CyclicList/Main.c index 1acc1a2..22921a9 100644 --- a/CyclicList/CyclicList/Main.c +++ b/CyclicList/CyclicList/Main.c @@ -8,7 +8,7 @@ int main() { setlocale(LC_ALL, "rus"); - if (!allTest() || !testFindSurviviorPosition()) + if (!allTest()) { printf(" "); return -1; diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index 412b421..51a70b7 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -1,5 +1,6 @@ #include "CyclicList.h" #include "TestCyclicList.h" +#include // Function to check the function that adds an item to the list bool testAdd() @@ -14,9 +15,12 @@ bool testAdd() deleteList(newList); return false; } - const int firstNumber = newList->head->value; - const int secondNumber = newList->head->next->value; - const int thirdNumber = newList->head->next->next->value; + Position* position = first(newList, &error); + const int firstNumber = get(position); + const int secondNumber = get(next(position)); + const int thirdNumber = get(next(position)); + printf("%d\n", thirdNumber); + free(position); deleteList(newList); return firstNumber == 1 && secondNumber == 123 && thirdNumber == 34; } @@ -36,14 +40,14 @@ bool testRemoveElement() } Position* position = first(newList, &error); next(position); - removeElement(newList, position, &error); + removeElement(newList, &position, &error); if (error == 1) { deleteList(newList); return false; } - const int firstNumber = newList->head->value; - const int secondNumber = newList->head->next->value; + const int firstNumber = get(newList, position); + const int secondNumber = get(newList, next(position)); free(position); deleteList(newList); return firstNumber == 10 && secondNumber == 30; @@ -51,5 +55,5 @@ bool testRemoveElement() bool allTest() { - return testRemoveElement() && testAdd(); + return testAdd(); } \ No newline at end of file From debdaffcbd7b79d0c76f2dc7ea2e8eee6b873012 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 5 Nov 2021 19:53:05 +0500 Subject: [PATCH 09/16] encapsulation has been done --- CyclicList/CyclicList/CyclicList.h | 4 ++++ CyclicList/CyclicList/Main.c | 2 +- CyclicList/CyclicList/TestCyclicList.c | 9 ++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CyclicList/CyclicList/CyclicList.h b/CyclicList/CyclicList/CyclicList.h index 1a036d6..a2907b2 100644 --- a/CyclicList/CyclicList/CyclicList.h +++ b/CyclicList/CyclicList/CyclicList.h @@ -32,10 +32,14 @@ Position* next(Position* position); // Function for moving to the previous position Position* previous(Position* position); +// Function that returns the value of the first element int returnFirstElementValue(List* list); +// Function that returns the value of an element int get(Position* position); +// Function that returns the first element ListElement* returnFirstElement(List* list); +// Function that returns the last element ListElement* returnLastElement(List* list); diff --git a/CyclicList/CyclicList/Main.c b/CyclicList/CyclicList/Main.c index 22921a9..1acc1a2 100644 --- a/CyclicList/CyclicList/Main.c +++ b/CyclicList/CyclicList/Main.c @@ -8,7 +8,7 @@ int main() { setlocale(LC_ALL, "rus"); - if (!allTest()) + if (!allTest() || !testFindSurviviorPosition()) { printf(" "); return -1; diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index 51a70b7..b84cfb3 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -19,7 +19,6 @@ bool testAdd() const int firstNumber = get(position); const int secondNumber = get(next(position)); const int thirdNumber = get(next(position)); - printf("%d\n", thirdNumber); free(position); deleteList(newList); return firstNumber == 1 && secondNumber == 123 && thirdNumber == 34; @@ -39,15 +38,15 @@ bool testRemoveElement() return false; } Position* position = first(newList, &error); - next(position); + position = next(position); removeElement(newList, &position, &error); if (error == 1) { deleteList(newList); return false; } - const int firstNumber = get(newList, position); - const int secondNumber = get(newList, next(position)); + const int firstNumber = get(position); + const int secondNumber = get(next(position)); free(position); deleteList(newList); return firstNumber == 10 && secondNumber == 30; @@ -55,5 +54,5 @@ bool testRemoveElement() bool allTest() { - return testAdd(); + return testAdd() && testRemoveElement; } \ No newline at end of file From bfade1e7bf9e45a03048bcc5dbb73083447d82a2 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 5 Nov 2021 19:59:45 +0500 Subject: [PATCH 10/16] fixed bugs with tests --- CyclicList/CyclicList/TestCyclicList.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index b84cfb3..32088d4 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -45,7 +45,7 @@ bool testRemoveElement() deleteList(newList); return false; } - const int firstNumber = get(position); + const int firstNumber = get(next(position)); const int secondNumber = get(next(position)); free(position); deleteList(newList); @@ -54,5 +54,5 @@ bool testRemoveElement() bool allTest() { - return testAdd() && testRemoveElement; + return testAdd() && testRemoveElement(); } \ No newline at end of file From 0cb42a96328b8128579ec58a720f8d3c0f336690 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 05:08:11 +0500 Subject: [PATCH 11/16] changing functions --- CyclicList/CyclicList/CircleOfMurders.c | 12 ++++--- CyclicList/CyclicList/CyclicList.c | 48 +++++++++++++------------ CyclicList/CyclicList/CyclicList.h | 23 +++++------- CyclicList/CyclicList/TestCyclicList.c | 4 +-- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c index 5f9e764..6725f85 100644 --- a/CyclicList/CyclicList/CircleOfMurders.c +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -1,5 +1,6 @@ #include "CircleOfMurders.h" #include "CyclicList.h" +#include int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error) { @@ -33,11 +34,11 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro return 0; } int counter = 1; - while (returnFirstElement(newList) != returnLastElement(newList)) + while (numberOfElements(newList) != 1) { if (counter % frequencyOfMurders == 0) { - removeElement(newList, &firstPosition, &errorCode); + removeElement(newList, firstPosition, &errorCode); if (errorCode == 1) { free(firstPosition); @@ -45,12 +46,15 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro *error = 1; return 0; } - previous(firstPosition); } - next(firstPosition); + else + { + next(firstPosition); + } counter++; } const int answer = returnFirstElementValue(newList); + free(firstPosition); deleteList(newList); return answer; } \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index 1b8884a..8dbb09c 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -1,9 +1,11 @@ #include "CyclicList.h" #include +#include // Structure containing pointers to the "first" and "last" list item typedef struct List { + int size; struct ListElement* head; struct ListElement* tail; } List; @@ -65,7 +67,7 @@ Position* previous(Position* position) return position; } -void invariant(ListElement* element) +void attach(ListElement* element) { element->next->previous = element; element->previous->next = element; @@ -80,6 +82,7 @@ void add(List* list, int value, int* error) *error = 3; return; } + list->size++; newElement->value = value; if (list->head == NULL) { @@ -91,7 +94,7 @@ void add(List* list, int value, int* error) } newElement->next = list->head; newElement->previous = list->tail; - invariant(newElement); + attach(newElement); list->tail = list->tail->next; } @@ -100,45 +103,51 @@ int returnFirstElementValue(List* list) return list->head->value; } -void removeElement(List* list, Position** position, int* error) +void removeElement(List* list, Position* position, int* error) { - if (list->head == NULL || list->tail == NULL) + if (list->head == NULL || position == NULL) { *error = 1; return; } - if ((*position)->position == list->head) + Position* newPosition = position; + if (position->position == list->head) { if (list->tail == list->head) { + free(list->head); + free(position); list->tail = NULL; list->head = NULL; - free(list->head); + list->size--; return; } ListElement* element = list->head; list->head = list->head->next; list->head->previous = list->tail; - invariant(list->head); - (*position)->position = list->head; + attach(list->head); + position->position = list->head; free(element); + list->size--; return; } - if ((*position)->position == list->tail) + if (position->position == list->tail) { ListElement* element = list->tail; list->tail = list->tail->previous; list->tail->next = list->head; - invariant(list->tail); - (*position)->position = list->head; + attach(list->tail); + position->position = list->head; free(element); + list->size--; return; } - ListElement* element = (*position)->position; - (*position)->position = (*position)->position->next; - (*position)->position->previous = (*position)->position->previous->previous; + ListElement* element = position->position; + position->position = position->position->next; + position->position->previous = position->position->previous->previous; free(element); - invariant((*position)->position); + list->size--; + attach(position->position); } int get(Position* position) @@ -146,12 +155,7 @@ int get(Position* position) return position->position->value; } -ListElement* returnFirstElement(List* list) -{ - return list->head; -} - -ListElement* returnLastElement(List* list) +int numberOfElements(List* list) { - return list->tail; + return list->size; } \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.h b/CyclicList/CyclicList/CyclicList.h index a2907b2..b48e4ee 100644 --- a/CyclicList/CyclicList/CyclicList.h +++ b/CyclicList/CyclicList/CyclicList.h @@ -1,14 +1,9 @@ #pragma once -#include -#include -// Structure containing pointers to the "first" and "last" list item +// Structure that represents cyclic list typedef struct List List; -// Structure containing pointers to the next and previous list item and a value variable for list items -typedef struct ListElement ListElement; - -// Structure containing a pointer to the position of a list item +// Structure for implementing a cyclic list typedef struct Position Position; // Function for creating a list @@ -20,16 +15,16 @@ void deleteList(List* list); // Function for adding an item to a list void add(List* list, int value, int* error); -// Function for the first position +// Function for finding the position of the first element Position* first(List* list, int* error); // Function to remove an item from the list -void removeElement(List* list, Position** position, int* error); +void removeElement(List* list, Position* position, int* error); -// Function for moving to the next position +// Function for moving to the position of the next element Position* next(Position* position); -// Function for moving to the previous position +// function for moving to the position of the previous element Position* previous(Position* position); // Function that returns the value of the first element @@ -38,8 +33,6 @@ int returnFirstElementValue(List* list); // Function that returns the value of an element int get(Position* position); -// Function that returns the first element -ListElement* returnFirstElement(List* list); +// Function for finding the number of items in the list +int numberOfElements(List* list); -// Function that returns the last element -ListElement* returnLastElement(List* list); diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index 32088d4..75f50eb 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -1,6 +1,6 @@ #include "CyclicList.h" #include "TestCyclicList.h" -#include +#include // Function to check the function that adds an item to the list bool testAdd() @@ -39,7 +39,7 @@ bool testRemoveElement() } Position* position = first(newList, &error); position = next(position); - removeElement(newList, &position, &error); + removeElement(newList, position, &error); if (error == 1) { deleteList(newList); From d5d468f46400cb27ad2f10ef4920afcca806ebbb Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 05:17:00 +0500 Subject: [PATCH 12/16] adding a function --- CyclicList/CyclicList/CircleOfMurders.c | 20 ++++++++------------ CyclicList/CyclicList/CyclicList.c | 5 +++++ CyclicList/CyclicList/CyclicList.h | 3 +++ CyclicList/CyclicList/TestCyclicList.c | 10 ++++++++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c index 6725f85..a3e4fa4 100644 --- a/CyclicList/CyclicList/CircleOfMurders.c +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -15,22 +15,19 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro return 0; } List* newList = createList(); - int errorCode = 0; for (int i = 1; i <= numberOfWarriors; i++) { - add(newList, i, &errorCode); - if (errorCode == 3) + add(newList, i, error); + if (*error == 3) { deleteList(newList); - *error = 3; return 0; } } - Position* firstPosition = first(newList, &errorCode); - if (errorCode == 3) + Position* firstPosition = first(newList, error); + if (*error == 3) { deleteList(newList); - *error = 3; return 0; } int counter = 1; @@ -38,12 +35,11 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro { if (counter % frequencyOfMurders == 0) { - removeElement(newList, firstPosition, &errorCode); - if (errorCode == 1) + removeElement(newList, firstPosition, error); + if (*error == 1) { - free(firstPosition); + freePosition(firstPosition); deleteList(newList); - *error = 1; return 0; } } @@ -54,7 +50,7 @@ int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* erro counter++; } const int answer = returnFirstElementValue(newList); - free(firstPosition); + freePosition(firstPosition); deleteList(newList); return answer; } \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index 8dbb09c..a227ab5 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -158,4 +158,9 @@ int get(Position* position) int numberOfElements(List* list) { return list->size; +} + +void freePosition(Position* position) +{ + free(position); } \ No newline at end of file diff --git a/CyclicList/CyclicList/CyclicList.h b/CyclicList/CyclicList/CyclicList.h index b48e4ee..6fe8614 100644 --- a/CyclicList/CyclicList/CyclicList.h +++ b/CyclicList/CyclicList/CyclicList.h @@ -36,3 +36,6 @@ int get(Position* position); // Function for finding the number of items in the list int numberOfElements(List* list); +// Function for freeing up memory +void freePosition(Position* position); + diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index 75f50eb..19400f4 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -19,7 +19,7 @@ bool testAdd() const int firstNumber = get(position); const int secondNumber = get(next(position)); const int thirdNumber = get(next(position)); - free(position); + freePosition(position); deleteList(newList); return firstNumber == 1 && secondNumber == 123 && thirdNumber == 34; } @@ -38,16 +38,22 @@ bool testRemoveElement() return false; } Position* position = first(newList, &error); + if (error == 3) + { + deleteList(newList); + return false; + } position = next(position); removeElement(newList, position, &error); if (error == 1) { + freePosition(position); deleteList(newList); return false; } const int firstNumber = get(next(position)); const int secondNumber = get(next(position)); - free(position); + freePosition(position); deleteList(newList); return firstNumber == 10 && secondNumber == 30; } From aec798e557725abce9d0919fbbd3d0da81092939 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 05:19:40 +0500 Subject: [PATCH 13/16] removed unnecessary libraries --- CyclicList/CyclicList/CircleOfMurders.c | 1 - CyclicList/CyclicList/TestCyclicList.c | 1 - 2 files changed, 2 deletions(-) diff --git a/CyclicList/CyclicList/CircleOfMurders.c b/CyclicList/CyclicList/CircleOfMurders.c index a3e4fa4..080a257 100644 --- a/CyclicList/CyclicList/CircleOfMurders.c +++ b/CyclicList/CyclicList/CircleOfMurders.c @@ -1,6 +1,5 @@ #include "CircleOfMurders.h" #include "CyclicList.h" -#include int findSurvivorPosition(int numberOfWarriors, int frequencyOfMurders, int* error) { diff --git a/CyclicList/CyclicList/TestCyclicList.c b/CyclicList/CyclicList/TestCyclicList.c index 19400f4..7069df4 100644 --- a/CyclicList/CyclicList/TestCyclicList.c +++ b/CyclicList/CyclicList/TestCyclicList.c @@ -1,6 +1,5 @@ #include "CyclicList.h" #include "TestCyclicList.h" -#include // Function to check the function that adds an item to the list bool testAdd() From 3f832a2f153afb7ebe5aff3aa40ebc5972cb722d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 05:27:36 +0500 Subject: [PATCH 14/16] add comments --- CyclicList/CyclicList/CyclicList.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index a227ab5..59e6d58 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -18,7 +18,7 @@ typedef struct ListElement struct ListElement* previous; } ListElement; -// Structure containing a pointer to the position of a list item +// A structure that stores a pointer to a list item typedef struct Position { ListElement* position; From 882c086c9012722db501237bdc29f93fa291555e Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 15:37:48 +0500 Subject: [PATCH 15/16] changing functions --- CyclicList/CyclicList/CyclicList.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CyclicList/CyclicList/CyclicList.c b/CyclicList/CyclicList/CyclicList.c index 59e6d58..2b7658e 100644 --- a/CyclicList/CyclicList/CyclicList.c +++ b/CyclicList/CyclicList/CyclicList.c @@ -38,6 +38,7 @@ void deleteList(List* list) free(position); position = list->head; } + list->size = 0; free(position); free(list); } @@ -75,23 +76,23 @@ void attach(ListElement* element) void add(List* list, int value, int* error) { - int errorCode = 0; ListElement* newElement = calloc(1, sizeof(ListElement)); if (newElement == NULL) { *error = 3; return; } - list->size++; newElement->value = value; if (list->head == NULL) { + list->size = 1; list->head = newElement; list->tail = newElement; list->tail->next = list->head; list->head->previous = list->tail; return; } + list->size++; newElement->next = list->head; newElement->previous = list->tail; attach(newElement); @@ -119,7 +120,7 @@ void removeElement(List* list, Position* position, int* error) free(position); list->tail = NULL; list->head = NULL; - list->size--; + list->size = 0; return; } ListElement* element = list->head; From 3890aaba39a9da625553aeead2f3085f3966b904 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 18:41:26 +0500 Subject: [PATCH 16/16] add commnets --- CyclicList/CyclicList/CyclicList.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CyclicList/CyclicList/CyclicList.h b/CyclicList/CyclicList/CyclicList.h index 6fe8614..792c771 100644 --- a/CyclicList/CyclicList/CyclicList.h +++ b/CyclicList/CyclicList/CyclicList.h @@ -15,16 +15,16 @@ void deleteList(List* list); // Function for adding an item to a list void add(List* list, int value, int* error); -// Function for finding the position of the first element +// Function for finding a pointer to the first element Position* first(List* list, int* error); // Function to remove an item from the list void removeElement(List* list, Position* position, int* error); -// Function for moving to the position of the next element +// Function for finding a pointer to the next element Position* next(Position* position); -// function for moving to the position of the previous element +// Function for finding a pointer to the previous element Position* previous(Position* position); // Function that returns the value of the first element