From d10292dfa92b22459858d294393146471ff2bfb7 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 03:21:45 +0500 Subject: [PATCH 01/24] Writing all functions and tests --- NewList/NewList.sln | 31 ++++ NewList/NewList/List.c | 180 ++++++++++++++++++++++++ NewList/NewList/List.h | 69 +++++++++ NewList/NewList/Main.c | 77 ++++++++++ NewList/NewList/MergeSort.c | 130 +++++++++++++++++ NewList/NewList/MergeSort.h | 6 + NewList/NewList/NewList.vcxproj | 157 +++++++++++++++++++++ NewList/NewList/NewList.vcxproj.filters | 48 +++++++ NewList/NewList/Phonebook.txt | 5 + NewList/NewList/TestList.c | 105 ++++++++++++++ NewList/NewList/TestList.h | 5 + NewList/NewList/TestMergeSort.c | 93 ++++++++++++ NewList/NewList/TestMergeSort.h | 5 + 13 files changed, 911 insertions(+) create mode 100644 NewList/NewList.sln create mode 100644 NewList/NewList/List.c create mode 100644 NewList/NewList/List.h create mode 100644 NewList/NewList/Main.c create mode 100644 NewList/NewList/MergeSort.c create mode 100644 NewList/NewList/MergeSort.h create mode 100644 NewList/NewList/NewList.vcxproj create mode 100644 NewList/NewList/NewList.vcxproj.filters create mode 100644 NewList/NewList/Phonebook.txt create mode 100644 NewList/NewList/TestList.c create mode 100644 NewList/NewList/TestList.h create mode 100644 NewList/NewList/TestMergeSort.c create mode 100644 NewList/NewList/TestMergeSort.h diff --git a/NewList/NewList.sln b/NewList/NewList.sln new file mode 100644 index 0000000..e7d3c04 --- /dev/null +++ b/NewList/NewList.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}") = "NewList", "NewList\NewList.vcxproj", "{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}" +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 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.ActiveCfg = Debug|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.Build.0 = Debug|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.ActiveCfg = Debug|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.Build.0 = Debug|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.ActiveCfg = Release|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.Build.0 = Release|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.ActiveCfg = Release|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3AA29672-FC57-4049-A441-2EC868F66725} + EndGlobalSection +EndGlobal diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c new file mode 100644 index 0000000..13fac02 --- /dev/null +++ b/NewList/NewList/List.c @@ -0,0 +1,180 @@ +#include "List.h" +#include +#include + +List* createList() +{ + return calloc(1, sizeof(List)); +} + +void deleteList(List* list) +{ + if (list->head == NULL) + { + return; + } + ListElement* position = list->head; + while (list->head != NULL) + { + list->head = list->head->next; + free(position); + position = list->head; + } + free(list); +} + +void deletePosition(Position* position, int* error) +{ + *error = 0; + if (position == NULL) + { + *error = 5; + return; + } + free(position); +} + +void removeFirstElement(List* list, int* error) +{ + *error = 0; + if (list-> head == NULL) + { + *error = 2; + return; + } + list->head = list->head->next; +} + +Position* first(List* list, int* error) +{ + *error = 0; + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = 3; + return NULL; + } + positionFirst->position = list->head; + return positionFirst; +} + +Position* last(List* list, int* error) +{ + *error = 0; + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = 3; + return NULL; + } + positionFirst->position = list->tail; + return positionFirst; +} + +Position* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +bool isLast(Position* position) +{ + return position->position == NULL; +} + +int numberOfElements(List* list, int* error) +{ + Position* position = first(list, &*error); + if (*error == 3) + { + return 0; + } + int counter = 1; + if (list->head == NULL) + { + return 0; + } + while (position->position->next != NULL) + { + position->position = position->position->next; + counter++; + } + return counter; +} + +int* getHeadFirstValue(List* list) +{ + return list->head->firstValue; +} + +int* getHeadSecondValue(List* list) +{ + return list->head->secondValue; +} + +void add(List* list, Position* position, int* firstValue, int* secondValue, int* error) +{ + int* fValue = calloc(100, sizeof(int)); + if (fValue == NULL) + { + *error = 3; + return; + } + int counter = 0; + while (firstValue[counter]) + { + fValue[counter] = firstValue[counter]; + counter++; + } + counter = 0; + int* sValue = calloc(100, sizeof(int)); + if (sValue == NULL) + { + *error = 3; + return; + } + while (secondValue[counter]) + { + sValue[counter] = secondValue[counter]; + counter++; + } + int errorCode = 0; + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + *error = 3; + return; + } + newElement->firstValue = fValue; + newElement->secondValue = sValue; + if (list->head == NULL) + { + list->head = newElement; + list->tail = newElement; + list->tail = list->head; + return; + } + position->position->next = newElement; + list->tail = list->tail->next; +} + +bool isEmpty(List* list) +{ + return list->head == NULL; +} + +bool isOneElement(List* list) +{ + return list->head->next == NULL; +} + +void print(List * list) +{ + int error = 0; + for (Position* position = first(list, &error); !isLast(position); position = next(position)) + { + printf("%s - ", (char*)(position->position->firstValue)); + printf("%s", (char*)(position->position->secondValue)); + printf("\n"); + } +} \ No newline at end of file diff --git a/NewList/NewList/List.h b/NewList/NewList/List.h new file mode 100644 index 0000000..d84f551 --- /dev/null +++ b/NewList/NewList/List.h @@ -0,0 +1,69 @@ +#pragma once +#include +#include + +// Structure containing pointers to the beginning and end of the list +typedef struct List +{ + struct ListElement* head; + struct ListElement* tail; +} List; + +// A structure containing a pointer to the next list item and a value variable for the list items +typedef struct ListElement +{ + int* firstValue; + int* secondValue; + struct ListElement* next; +} ListElement; + +// A 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 deleting a position +void deletePosition(Position* position, int* error); + +// Function for adding an item to a list +void add(List* list, Position* position, int* firstValue, int* secondValue , int* error); + +// Function for the first position +Position* first(List* list, int* error); + +// Function for moving to the next position +Position* next(Position* position); + +// Function for checking an item for being at the end of the list +bool isLast(Position* position); + +// Function for printing a list +void print(List* list); + +// Function for deleting the first element +void removeFirstElement(List* list, int* error); + +// Function for finding the number of items in the list +int numberOfElements(List* list, int* error); + +// Function for checking the emptiness of the list +bool isEmpty(List* list); + +// Function for checking the list for the content of a single element +bool isOneElement(List* list); + +// Function that returns a value for the first element +int* getHeadFirstValue(List* list); + +// Function that returns a value for the first element +int* getHeadSecondValue(List* list); + +// Function for finding the position of the last element +Position* last(List* list, int* error); diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c new file mode 100644 index 0000000..af1f522 --- /dev/null +++ b/NewList/NewList/Main.c @@ -0,0 +1,77 @@ +#include "List.h" +#include "MergeSort.h" +#include "TestList.h" +#include "TestMergeSort.h" +#include +#include +#include + +void readPhonebook(List* list, const char* fileName, int* error) +{ + FILE* file = fopen(fileName, "r"); + if (file == NULL) + { + *error = 1; + return; + } + while (!feof(file)) + { + char arrayForName[100] = {'\0'}; + char arrayForNumber[100] = {'\0'}; + if (fscanf(file, "%s", arrayForName) != EOF); + { + const int fscanfResult = fscanf(file, "%s", arrayForNumber); + if (fscanfResult == 0) + { + break; + } + } + add(list, last(list, &*error), (int*)arrayForName, (int*)arrayForNumber, &*error); + if (*error == 3) + { + return; + } + } + fclose(file); +} + +int main() +{ + setlocale(LC_ALL, "rus"); + if (!allTest() || !testMergeSort()) + { + printf(" "); + return -1; + } + List* newList = createList(); + int error = 0; + readPhonebook(newList, "Phonebook.txt", &error); + printf(" ?\n"); + printf(" 1 , \n"); + int number = 0; + const int scanResult = scanf("%d", &number); + if (scanResult == 0) + { + printf(" "); + return -1; + } + List* answer = mergeSort(newList, number, &error); + if (error == 1) + { + printf(" "); + return -1; + } + if (error == 2) + { + printf(" "); + return -1; + } + if (error == 3) + { + printf(" "); + return -1; + } + print(answer); + printf("\n"); + deleteList(answer); +} \ No newline at end of file diff --git a/NewList/NewList/MergeSort.c b/NewList/NewList/MergeSort.c new file mode 100644 index 0000000..44329de --- /dev/null +++ b/NewList/NewList/MergeSort.c @@ -0,0 +1,130 @@ +#include "MergeSort.h" +#include + +bool compareTheHeads(List* firstList, List* secondlist, int number) +{ + if (number == 1) + { + return strcmp((char*)getHeadFirstValue(firstList), (char*)getHeadFirstValue(secondlist)) <= 0; + } + else + { + return strcmp((char*)getHeadSecondValue(firstList), (char*)getHeadSecondValue(secondlist)) <= 0; + } +} + +List* merge(List* firstList, List* secondList, int number, int* error) +{ + List* sortedList = createList(); + while (!isEmpty(firstList) && !isEmpty(secondList)) + { + if (compareTheHeads(firstList, secondList, number)) + { + add(sortedList, last(sortedList, &*error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), &*error); + if (*error == 3) + { + return NULL; + } + removeFirstElement(firstList, &*error); + if (*error == 2) + { + return NULL; + } + } + else + { + add(sortedList, last(sortedList, &*error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), &*error); + removeFirstElement(secondList, &*error); + if (*error == 2) + { + return NULL; + } + } + } + while (!isEmpty(firstList)) + { + add(sortedList, last(sortedList, &*error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), &*error); + if (*error == 3) + { + return NULL; + } + removeFirstElement(firstList, &*error); + if (*error == 2) + { + return NULL; + } + } + while (!isEmpty(secondList)) + { + add(sortedList, last(sortedList, &*error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), &*error); + if (*error == 3) + { + return NULL; + } + removeFirstElement(secondList, &*error); + if (*error == 2) + { + return NULL; + } + } + deleteList(firstList); + deleteList(secondList); + return sortedList; +} + +List* mergeSort(List* list, int number, int* error) +{ + if (isEmpty(list)) + { + return list; + } + if (isOneElement(list)) + { + return list; + } + const int leftBorderOfList = numberOfElements(list, &*error) / 2; + if (*error == 3) + { + return NULL; + } + const int rightBorderOfList = numberOfElements(list, &*error); + if (*error == 3) + { + return NULL; + } + List* leftHalfOfTheList = createList(); + List* rightHalfOfTheList = createList(); + for (int i = 1; i <= leftBorderOfList; i++) + { + add(leftHalfOfTheList, last(leftHalfOfTheList, &*error), getHeadFirstValue(list), getHeadSecondValue(list), &*error); + if (*error == 3) + { + return NULL; + } + removeFirstElement(list, &*error); + if (*error == 2) + { + return NULL; + } + } + for (int i = leftBorderOfList + 1; i <= rightBorderOfList; i++) + { + add(rightHalfOfTheList, last(rightHalfOfTheList, &*error), getHeadFirstValue(list), getHeadSecondValue(list), &*error); + if (*error == 3) + { + return NULL; + } + removeFirstElement(list, &*error); + if (*error == 2) + { + return NULL; + } + } + deleteList(list); + List* firstList = mergeSort(leftHalfOfTheList, number, &*error); + List* secondList = mergeSort(rightHalfOfTheList, number, &*error); + List* answer = merge(firstList, secondList, number, &*error); + deleteList(firstList); + deleteList(secondList); + return answer; +} \ No newline at end of file diff --git a/NewList/NewList/MergeSort.h b/NewList/NewList/MergeSort.h new file mode 100644 index 0000000..fa20f38 --- /dev/null +++ b/NewList/NewList/MergeSort.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include "List.h" + +// Function for merge sorting +List* mergeSort(List* list, int number, int* error); diff --git a/NewList/NewList/NewList.vcxproj b/NewList/NewList/NewList.vcxproj new file mode 100644 index 0000000..4fe7da0 --- /dev/null +++ b/NewList/NewList/NewList.vcxproj @@ -0,0 +1,157 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {8b99b0f3-fcb6-4875-8c2f-e0a7069b6043} + NewList + 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;_CRT_SECURE_NO_WARNINGS;_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/NewList/NewList/NewList.vcxproj.filters b/NewList/NewList/NewList.vcxproj.filters new file mode 100644 index 0000000..2c57a68 --- /dev/null +++ b/NewList/NewList/NewList.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/NewList/NewList/Phonebook.txt b/NewList/NewList/Phonebook.txt new file mode 100644 index 0000000..8db74ad --- /dev/null +++ b/NewList/NewList/Phonebook.txt @@ -0,0 +1,5 @@ +Pavel 89072 +Sasha 21312 +Artem 62513 +Ruslan 41211 +Nikolay 123123 \ No newline at end of file diff --git a/NewList/NewList/TestList.c b/NewList/NewList/TestList.c new file mode 100644 index 0000000..8db0687 --- /dev/null +++ b/NewList/NewList/TestList.c @@ -0,0 +1,105 @@ +#include "TestList.h" +#include "List.h" +#include + +// Function to check the function that adds an item to the list +bool testAdd() +{ + int error = 0; + List* newList = createList(); + add(newList, last(newList, &error), (int*)"test", (int*)" 1", & error); + add(newList, last(newList, &error), (int*)"list", (int*)"add", & error); + if (error == 3) + { + return false; + } + const int* firstStringFirstValue = newList->head->firstValue; + const int* firstStringSecondValue = newList->head->secondValue; + const int* secondStringFirstValue = newList->head->next->firstValue; + const int* secondStringSecondValue = newList->head->next->secondValue; + deleteList(newList); + return strcmp((char*)firstStringFirstValue, "test") == 0 && strcmp((char*)firstStringSecondValue, " 1") == 0 + && strcmp((char*)secondStringFirstValue, "list") == 0 && strcmp((char*)secondStringSecondValue, "add") == 0; +} + +// Function to check the function that deletes the first item in the list +bool testRemoveHead() +{ + int error = 0; + List* newList = createList(); + add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); + add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); + if (error == 3) + { + return false; + } + removeFirstElement(newList, &error); + if (error == 2) + { + return false; + } + const int* headOfListFirstValue = newList->head->firstValue; + const int* headOfListSecondValue = newList->head->secondValue; + deleteList(newList); + return strcmp((char*)headOfListFirstValue, "Merge") == 0 && strcmp((char*)headOfListSecondValue, "Sort") == 0; +} + +// Function for checking the function counting the number of elements +bool testNumberOfElements() +{ + List* newList = createList(); + int error = 0; + add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); + if (error == 3) + { + return false; + } + add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); + if (error == 3) + { + return false; + } + add(newList, last(newList, &error), (int*)"test", (int*)" 1", &error); + if (error == 3) + { + return false; + } + add(newList, last(newList, &error), (int*)"list", (int*)"add", &error); + if (error == 3) + { + return false; + } + const int number = numberOfElements(newList, &error); + if (error == 3) + { + return false; + } + deleteList(newList); + return number == 4; +} + +// Function that checks the function that returns the value of the first element +bool testGetHeadFirstAndSecondValue() +{ + List* newList = createList(); + int error = 0; + add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); + if (error == 3) + { + return false; + } + add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); + if (error == 3) + { + return false; + } + const int* firstHeadValue = getHeadFirstValue(newList); + const int* secondHeadValue = getHeadSecondValue(newList); + deleteList(newList); + return strcmp((char*)firstHeadValue, "Hello") == 0 && strcmp((char*)secondHeadValue, "World") == 0; +} + +bool allTest() +{ + return testAdd() && testNumberOfElements() && testRemoveHead() && testGetHeadFirstAndSecondValue(); +} \ No newline at end of file diff --git a/NewList/NewList/TestList.h b/NewList/NewList/TestList.h new file mode 100644 index 0000000..13ded01 --- /dev/null +++ b/NewList/NewList/TestList.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// A function that combines the results of all tests +bool allTest(); diff --git a/NewList/NewList/TestMergeSort.c b/NewList/NewList/TestMergeSort.c new file mode 100644 index 0000000..a17990f --- /dev/null +++ b/NewList/NewList/TestMergeSort.c @@ -0,0 +1,93 @@ +#include "TestMergeSort.h" +#include "MergeSort.h" +#include "List.h" +#include + +bool testMergeSort() +{ + List* newFirstList = createList(); + int error = 0; + add(newFirstList, last(newFirstList, &error), (int*)"Pavel", (int*)"890345", &error); + if (error == 3) + { + return false; + } + add(newFirstList, last(newFirstList, &error), (int*)"Alex", (int*)"765494", &error); + if (error == 3) + { + return false; + } + add(newFirstList, last(newFirstList, &error), (int*)"Artem", (int*)"956469", &error); + if (error == 3) + { + return false; + } + add(newFirstList, last(newFirstList, &error), (int*)"Ruslan", (int*)"2342424", &error); + if (error == 3) + { + return false; + } + List* firstList = mergeSort(newFirstList, 1, &error); + + const char* firstStringFirstValueFirstList = (char*)firstList->head->firstValue; + const char* firstStringSecondValueFirstList = (char*)firstList->head->secondValue; + + const char* secondStringFirstValueFirstList = (char*)firstList->head->next->firstValue; + const char* secondStringSecondValueFirstList = (char*)firstList->head->next->secondValue; + + const char* thirdStringFirstValueFirstList = (char*)firstList->head->next->next->firstValue; + const char* thirdStringSecondValueFirstList = (char*)firstList->head->next->next->secondValue; + + const char* fourthStringFirstValueFirstList = (char*)firstList->head->next->next->next->firstValue; + const char* fourthStringSecondValueFirstList = (char*)firstList->head->next->next->next->secondValue; + + List* newSecondList = createList(); + add(newSecondList, last(newSecondList, &error), (int*)"Pavel", (int*)"890345", &error); + if (error == 3) + { + return false; + } + add(newSecondList, last(newSecondList, &error), (int*)"Alex", (int*)"765494", &error); + if (error == 3) + { + return false; + } + add(newSecondList, last(newSecondList, &error), (int*)"Artem", (int*)"956469", &error); + if (error == 3) + { + return false; + } + add(newSecondList, last(newSecondList, &error), (int*)"Ruslan", (int*)"2342424", &error); + if (error == 3) + { + return false; + } + List* secondList = mergeSort(newSecondList, 0, &error); + + const char* firstStringFirstValueSecondList = (char*)secondList->head->firstValue; + const char* firstStringSecondValueSecondList = (char*)secondList->head->secondValue; + + const char* secondStringFirstValueSecondList = (char*)secondList->head->next->firstValue; + const char* secondStringSecondValueSecondList = (char*)secondList->head->next->secondValue; + + const char* thirdStringFirstValueSecondList = (char*)secondList->head->next->next->firstValue; + const char* thirdStringSecondValueSecondList = (char*)secondList->head->next->next->secondValue; + + const char* fourthStringFirstValueSecondList = (char*)secondList->head->next->next->next->firstValue; + const char* fourthStringSecondValueSecondList = (char*)secondList->head->next->next->next->secondValue; + + deleteList(newFirstList); + deleteList(newSecondList); + deleteList(firstList); + deleteList(secondList); + + return strcmp(firstStringFirstValueFirstList, "Alex") == 0 && strcmp(firstStringSecondValueFirstList, "765494") == 0 + && strcmp(secondStringFirstValueFirstList, "Artem") == 0 && strcmp(secondStringSecondValueFirstList, "956469") == 0 + && strcmp(thirdStringFirstValueFirstList, "Pavel") == 0 && strcmp(thirdStringSecondValueFirstList, "890345") == 0 + && strcmp(fourthStringFirstValueFirstList, "Ruslan") == 0 && strcmp(fourthStringSecondValueFirstList, "2342424") == 0 + + && strcmp(firstStringFirstValueSecondList, "Ruslan") == 0 && strcmp(firstStringSecondValueSecondList, "2342424") == 0 + && strcmp(secondStringFirstValueSecondList, "Alex") == 0 && strcmp(secondStringSecondValueSecondList, "765494") == 0 + && strcmp(thirdStringFirstValueSecondList, "Pavel") == 0 && strcmp(thirdStringSecondValueSecondList, "890345") == 0 + && strcmp(fourthStringFirstValueSecondList, "Artem") == 0 && strcmp(fourthStringSecondValueSecondList, "956469") == 0; +} \ No newline at end of file diff --git a/NewList/NewList/TestMergeSort.h b/NewList/NewList/TestMergeSort.h new file mode 100644 index 0000000..9ed03fc --- /dev/null +++ b/NewList/NewList/TestMergeSort.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// Function for checking the merge sort function +bool testMergeSort(); \ No newline at end of file From 0b08d6990230ce38f9836265f8d3de1b136b9fe9 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 2 Nov 2021 17:31:40 +0500 Subject: [PATCH 02/24] fixed memory bugs --- NewList/NewList/Main.c | 4 ++++ NewList/NewList/TestList.c | 10 +++++++++ NewList/NewList/TestMergeSort.c | 40 ++++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c index af1f522..6322308 100644 --- a/NewList/NewList/Main.c +++ b/NewList/NewList/Main.c @@ -56,18 +56,22 @@ int main() return -1; } List* answer = mergeSort(newList, number, &error); + deleteList(newList); if (error == 1) { + deleteList(answer); printf(" "); return -1; } if (error == 2) { + deleteList(answer); printf(" "); return -1; } if (error == 3) { + deleteList(answer); printf(" "); return -1; } diff --git a/NewList/NewList/TestList.c b/NewList/NewList/TestList.c index 8db0687..3288dad 100644 --- a/NewList/NewList/TestList.c +++ b/NewList/NewList/TestList.c @@ -11,6 +11,7 @@ bool testAdd() add(newList, last(newList, &error), (int*)"list", (int*)"add", & error); if (error == 3) { + deleteList(newList); return false; } const int* firstStringFirstValue = newList->head->firstValue; @@ -31,11 +32,13 @@ bool testRemoveHead() add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); if (error == 3) { + deleteList(newList); return false; } removeFirstElement(newList, &error); if (error == 2) { + deleteList(newList); return false; } const int* headOfListFirstValue = newList->head->firstValue; @@ -52,26 +55,31 @@ bool testNumberOfElements() add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); if (error == 3) { + deleteList(newList); return false; } add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); if (error == 3) { + deleteList(newList); return false; } add(newList, last(newList, &error), (int*)"test", (int*)" 1", &error); if (error == 3) { + deleteList(newList); return false; } add(newList, last(newList, &error), (int*)"list", (int*)"add", &error); if (error == 3) { + deleteList(newList); return false; } const int number = numberOfElements(newList, &error); if (error == 3) { + deleteList(newList); return false; } deleteList(newList); @@ -86,11 +94,13 @@ bool testGetHeadFirstAndSecondValue() add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); if (error == 3) { + deleteList(newList); return false; } add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); if (error == 3) { + deleteList(newList); return false; } const int* firstHeadValue = getHeadFirstValue(newList); diff --git a/NewList/NewList/TestMergeSort.c b/NewList/NewList/TestMergeSort.c index a17990f..4c16286 100644 --- a/NewList/NewList/TestMergeSort.c +++ b/NewList/NewList/TestMergeSort.c @@ -10,58 +10,72 @@ bool testMergeSort() add(newFirstList, last(newFirstList, &error), (int*)"Pavel", (int*)"890345", &error); if (error == 3) { + deleteList(newFirstList); return false; } add(newFirstList, last(newFirstList, &error), (int*)"Alex", (int*)"765494", &error); if (error == 3) { + deleteList(newFirstList); return false; } add(newFirstList, last(newFirstList, &error), (int*)"Artem", (int*)"956469", &error); if (error == 3) { + deleteList(newFirstList); return false; } add(newFirstList, last(newFirstList, &error), (int*)"Ruslan", (int*)"2342424", &error); if (error == 3) { + deleteList(newFirstList); return false; } - List* firstList = mergeSort(newFirstList, 1, &error); - - const char* firstStringFirstValueFirstList = (char*)firstList->head->firstValue; - const char* firstStringSecondValueFirstList = (char*)firstList->head->secondValue; - - const char* secondStringFirstValueFirstList = (char*)firstList->head->next->firstValue; - const char* secondStringSecondValueFirstList = (char*)firstList->head->next->secondValue; - - const char* thirdStringFirstValueFirstList = (char*)firstList->head->next->next->firstValue; - const char* thirdStringSecondValueFirstList = (char*)firstList->head->next->next->secondValue; - - const char* fourthStringFirstValueFirstList = (char*)firstList->head->next->next->next->firstValue; - const char* fourthStringSecondValueFirstList = (char*)firstList->head->next->next->next->secondValue; List* newSecondList = createList(); add(newSecondList, last(newSecondList, &error), (int*)"Pavel", (int*)"890345", &error); if (error == 3) { + deleteList(newSecondList); + deleteList(newFirstList); return false; } add(newSecondList, last(newSecondList, &error), (int*)"Alex", (int*)"765494", &error); if (error == 3) { + deleteList(newSecondList); + deleteList(newFirstList); return false; } add(newSecondList, last(newSecondList, &error), (int*)"Artem", (int*)"956469", &error); if (error == 3) { + deleteList(newSecondList); + deleteList(newFirstList); return false; } add(newSecondList, last(newSecondList, &error), (int*)"Ruslan", (int*)"2342424", &error); if (error == 3) { + deleteList(newSecondList); + deleteList(newFirstList); return false; } + + List* firstList = mergeSort(newFirstList, 1, &error); + + const char* firstStringFirstValueFirstList = (char*)firstList->head->firstValue; + const char* firstStringSecondValueFirstList = (char*)firstList->head->secondValue; + + const char* secondStringFirstValueFirstList = (char*)firstList->head->next->firstValue; + const char* secondStringSecondValueFirstList = (char*)firstList->head->next->secondValue; + + const char* thirdStringFirstValueFirstList = (char*)firstList->head->next->next->firstValue; + const char* thirdStringSecondValueFirstList = (char*)firstList->head->next->next->secondValue; + + const char* fourthStringFirstValueFirstList = (char*)firstList->head->next->next->next->firstValue; + const char* fourthStringSecondValueFirstList = (char*)firstList->head->next->next->next->secondValue; + List* secondList = mergeSort(newSecondList, 0, &error); const char* firstStringFirstValueSecondList = (char*)secondList->head->firstValue; From 00f3204488dd70b52473973f650ad4f608b19f00 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 15:32:43 +0500 Subject: [PATCH 03/24] started changing functions --- NewList/NewList/List.c | 99 ++++++++++++++++++++---------------------- NewList/NewList/List.h | 26 +++-------- 2 files changed, 52 insertions(+), 73 deletions(-) diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c index 13fac02..e3dfe3d 100644 --- a/NewList/NewList/List.c +++ b/NewList/NewList/List.c @@ -2,6 +2,29 @@ #include #include + +// Structure containing pointers to the beginning and end of the list +typedef struct List +{ + int size; + struct ListElement* head; + struct ListElement* tail; +} List; + +// A structure containing a pointer to the next list item and a value variable for the list items +typedef struct ListElement +{ + char* firstValue; + char* secondValue; + struct ListElement* next; +} ListElement; + +// A structure containing a pointer to the position of a list item +typedef struct Position +{ + ListElement* position; +} Position; + List* createList() { return calloc(1, sizeof(List)); @@ -9,12 +32,8 @@ List* createList() void deleteList(List* list) { - if (list->head == NULL) - { - return; - } ListElement* position = list->head; - while (list->head != NULL) + while (position != NULL) { list->head = list->head->next; free(position); @@ -23,26 +42,23 @@ void deleteList(List* list) free(list); } -void deletePosition(Position* position, int* error) +void deletePosition(Position* position) { - *error = 0; - if (position == NULL) - { - *error = 5; - return; - } free(position); } void removeFirstElement(List* list, int* error) { - *error = 0; - if (list-> head == NULL) + if (list->head == NULL) { - *error = 2; return; } + ListElement* position = list->head; list->head = list->head->next; + list->size--; + free(position->firstValue); + free(position->secondValue); + free(position); } Position* first(List* list, int* error) @@ -79,27 +95,12 @@ Position* next(Position* position) bool isLast(Position* position) { - return position->position == NULL; + return position->position->next == NULL; } int numberOfElements(List* list, int* error) { - Position* position = first(list, &*error); - if (*error == 3) - { - return 0; - } - int counter = 1; - if (list->head == NULL) - { - return 0; - } - while (position->position->next != NULL) - { - position->position = position->position->next; - counter++; - } - return counter; + return list->size; } int* getHeadFirstValue(List* list) @@ -112,48 +113,42 @@ int* getHeadSecondValue(List* list) return list->head->secondValue; } -void add(List* list, Position* position, int* firstValue, int* secondValue, int* error) +void add(List* list, Position* position, char* firstValue, char* secondValue, int* error) { - int* fValue = calloc(100, sizeof(int)); - if (fValue == NULL) + char* firstValueCopy = calloc(strlen(firstValue), sizeof(char)); + if (firstValueCopy == NULL) { *error = 3; return; } - int counter = 0; - while (firstValue[counter]) - { - fValue[counter] = firstValue[counter]; - counter++; - } - counter = 0; - int* sValue = calloc(100, sizeof(int)); - if (sValue == NULL) + strcpy(firstValueCopy, firstValue); + int* secondValueCopy = calloc(strlen(secondValue), sizeof(int)); + if (secondValueCopy == NULL) { + free(firstValueCopy); *error = 3; return; } - while (secondValue[counter]) - { - sValue[counter] = secondValue[counter]; - counter++; - } - int errorCode = 0; + strcpy(secondValueCopy, firstValue); ListElement* newElement = calloc(1, sizeof(ListElement)); if (newElement == NULL) { + free(firstValueCopy); + free(secondValueCopy); *error = 3; return; } - newElement->firstValue = fValue; - newElement->secondValue = sValue; + newElement->firstValue = firstValueCopy; + newElement->secondValue = secondValueCopy; if (list->head == NULL) { + list->size = 1; list->head = newElement; list->tail = newElement; list->tail = list->head; return; } + list->size++; position->position->next = newElement; list->tail = list->tail->next; } diff --git a/NewList/NewList/List.h b/NewList/NewList/List.h index d84f551..5e991dc 100644 --- a/NewList/NewList/List.h +++ b/NewList/NewList/List.h @@ -3,25 +3,9 @@ #include // Structure containing pointers to the beginning and end of the list -typedef struct List -{ - struct ListElement* head; - struct ListElement* tail; -} List; - -// A structure containing a pointer to the next list item and a value variable for the list items -typedef struct ListElement -{ - int* firstValue; - int* secondValue; - struct ListElement* next; -} ListElement; - -// A structure containing a pointer to the position of a list item -typedef struct Position -{ - ListElement* position; -} Position; +typedef struct List List; + +typedef struct Position Position; // Function for creating a list List* createList(); @@ -30,10 +14,10 @@ List* createList(); void deleteList(List* list); // Function for deleting a position -void deletePosition(Position* position, int* error); +void deletePosition(Position* position); // Function for adding an item to a list -void add(List* list, Position* position, int* firstValue, int* secondValue , int* error); +void add(List* list, Position* position, char* firstValue, char* secondValue , int* error); // Function for the first position Position* first(List* list, int* error); From c2e4c8431dbe1ec50f31ce94b3194596874b494a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 18:34:27 +0500 Subject: [PATCH 04/24] adding features --- NewList/NewList/List.c | 73 +++++++++++++++++++++------- NewList/NewList/List.h | 19 +++++--- NewList/NewList/Main.c | 4 +- NewList/NewList/MergeSort.c | 46 ++++++++---------- NewList/NewList/MergeSort.h | 1 - NewList/NewList/TestList.c | 52 +++++++++++--------- NewList/NewList/TestMergeSort.c | 84 ++++++++++++++++++--------------- 7 files changed, 163 insertions(+), 116 deletions(-) diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c index e3dfe3d..0bf76c2 100644 --- a/NewList/NewList/List.c +++ b/NewList/NewList/List.c @@ -1,6 +1,8 @@ #include "List.h" #include #include +#include +#include // Structure containing pointers to the beginning and end of the list @@ -19,7 +21,7 @@ typedef struct ListElement struct ListElement* next; } ListElement; -// A structure containing a pointer to the position of a list item +// contains a pointer to a ListElement typedef struct Position { ListElement* position; @@ -32,13 +34,22 @@ List* createList() void deleteList(List* list) { + if (list->head == NULL) + { + list->size = 0; + free(list); + return; + } ListElement* position = list->head; while (position != NULL) { list->head = list->head->next; + free(position->secondValue); + free(position->firstValue); free(position); position = list->head; } + list->size = 0; free(list); } @@ -49,15 +60,17 @@ void deletePosition(Position* position) void removeFirstElement(List* list, int* error) { + *error = 0; if (list->head == NULL) { + *error = 2; return; } ListElement* position = list->head; list->head = list->head->next; list->size--; - free(position->firstValue); free(position->secondValue); + free(position->firstValue); free(position); } @@ -77,14 +90,14 @@ Position* first(List* list, int* error) Position* last(List* list, int* error) { *error = 0; - Position* positionFirst = malloc(sizeof(Position)); - if (positionFirst == NULL) + Position* positionLast = malloc(sizeof(Position)); + if (positionLast == NULL) { *error = 3; return NULL; } - positionFirst->position = list->tail; - return positionFirst; + positionLast->position = list->tail; + return positionLast; } Position* next(Position* position) @@ -93,43 +106,69 @@ Position* next(Position* position) return position; } -bool isLast(Position* position) +bool isLastElement(Position* position) { return position->position->next == NULL; } -int numberOfElements(List* list, int* error) +int numberOfElements(List* list) { return list->size; } -int* getHeadFirstValue(List* list) +char* getHeadFirstValue(List* list) { + if (list->head == NULL) + { + return NULL; + } return list->head->firstValue; } -int* getHeadSecondValue(List* list) +char* getHeadSecondValue(List* list) { + if (list->head == NULL) + { + return NULL; + } return list->head->secondValue; } +char* getFirstValue(Position* position) +{ + if (position->position == NULL) + { + return NULL; + } + return position->position->firstValue; +} + +char* getSecondValue(Position* position) +{ + if (position->position == NULL) + { + return NULL; + } + return position->position->secondValue; +} + void add(List* list, Position* position, char* firstValue, char* secondValue, int* error) { - char* firstValueCopy = calloc(strlen(firstValue), sizeof(char)); + char* firstValueCopy = calloc(100, sizeof(char)); if (firstValueCopy == NULL) { *error = 3; return; } strcpy(firstValueCopy, firstValue); - int* secondValueCopy = calloc(strlen(secondValue), sizeof(int)); + char* secondValueCopy = calloc(100, sizeof(char)); if (secondValueCopy == NULL) { free(firstValueCopy); *error = 3; return; } - strcpy(secondValueCopy, firstValue); + strcpy(secondValueCopy, secondValue); ListElement* newElement = calloc(1, sizeof(ListElement)); if (newElement == NULL) { @@ -163,13 +202,13 @@ bool isOneElement(List* list) return list->head->next == NULL; } -void print(List * list) +void print(List* list) { int error = 0; - for (Position* position = first(list, &error); !isLast(position); position = next(position)) + for (Position* position = first(list, &error); !isLastElement(position); position = next(position)) { - printf("%s - ", (char*)(position->position->firstValue)); - printf("%s", (char*)(position->position->secondValue)); + printf("%s - ", position->position->firstValue); + printf("%s", position->position->secondValue); printf("\n"); } } \ No newline at end of file diff --git a/NewList/NewList/List.h b/NewList/NewList/List.h index 5e991dc..85a961e 100644 --- a/NewList/NewList/List.h +++ b/NewList/NewList/List.h @@ -1,10 +1,10 @@ #pragma once #include -#include -// Structure containing pointers to the beginning and end of the list +// Structure that represents list typedef struct List List; +// Structure that represents list typedef struct Position Position; // Function for creating a list @@ -19,14 +19,15 @@ void deletePosition(Position* position); // Function for adding an item to a list void add(List* list, Position* position, char* firstValue, char* secondValue , int* error); -// Function for the first position +// function to find a pointer to the first element + Position* first(List* list, int* error); // Function for moving to the next position Position* next(Position* position); // Function for checking an item for being at the end of the list -bool isLast(Position* position); +bool isLastElement(Position* position); // Function for printing a list void print(List* list); @@ -35,7 +36,7 @@ void print(List* list); void removeFirstElement(List* list, int* error); // Function for finding the number of items in the list -int numberOfElements(List* list, int* error); +int numberOfElements(List* list); // Function for checking the emptiness of the list bool isEmpty(List* list); @@ -44,10 +45,14 @@ bool isEmpty(List* list); bool isOneElement(List* list); // Function that returns a value for the first element -int* getHeadFirstValue(List* list); +char* getHeadFirstValue(List* list); // Function that returns a value for the first element -int* getHeadSecondValue(List* list); +char* getHeadSecondValue(List* list); // Function for finding the position of the last element Position* last(List* list, int* error); + +char* getFirstValue(Position* position); + +char* getSecondValue(Position* position); diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c index 6322308..d547b64 100644 --- a/NewList/NewList/Main.c +++ b/NewList/NewList/Main.c @@ -3,7 +3,6 @@ #include "TestList.h" #include "TestMergeSort.h" #include -#include #include void readPhonebook(List* list, const char* fileName, int* error) @@ -26,9 +25,10 @@ void readPhonebook(List* list, const char* fileName, int* error) break; } } - add(list, last(list, &*error), (int*)arrayForName, (int*)arrayForNumber, &*error); + add(list, last(list, error), arrayForName, arrayForNumber, error); if (*error == 3) { + fclose(file); return; } } diff --git a/NewList/NewList/MergeSort.c b/NewList/NewList/MergeSort.c index 44329de..a99c489 100644 --- a/NewList/NewList/MergeSort.c +++ b/NewList/NewList/MergeSort.c @@ -5,11 +5,11 @@ bool compareTheHeads(List* firstList, List* secondlist, int number) { if (number == 1) { - return strcmp((char*)getHeadFirstValue(firstList), (char*)getHeadFirstValue(secondlist)) <= 0; + return strcmp(getHeadFirstValue(firstList), getHeadFirstValue(secondlist)) <= 0; } else { - return strcmp((char*)getHeadSecondValue(firstList), (char*)getHeadSecondValue(secondlist)) <= 0; + return strcmp(getHeadSecondValue(firstList), getHeadSecondValue(secondlist)) <= 0; } } @@ -20,12 +20,12 @@ List* merge(List* firstList, List* secondList, int number, int* error) { if (compareTheHeads(firstList, secondList, number)) { - add(sortedList, last(sortedList, &*error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), &*error); + add(sortedList, last(sortedList, error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { return NULL; } - removeFirstElement(firstList, &*error); + removeFirstElement(firstList, error); if (*error == 2) { return NULL; @@ -33,8 +33,8 @@ List* merge(List* firstList, List* secondList, int number, int* error) } else { - add(sortedList, last(sortedList, &*error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), &*error); - removeFirstElement(secondList, &*error); + add(sortedList, last(sortedList, error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); + removeFirstElement(secondList, error); if (*error == 2) { return NULL; @@ -43,12 +43,12 @@ List* merge(List* firstList, List* secondList, int number, int* error) } while (!isEmpty(firstList)) { - add(sortedList, last(sortedList, &*error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), &*error); + add(sortedList, last(sortedList, error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { return NULL; } - removeFirstElement(firstList, &*error); + removeFirstElement(firstList, error); if (*error == 2) { return NULL; @@ -56,12 +56,12 @@ List* merge(List* firstList, List* secondList, int number, int* error) } while (!isEmpty(secondList)) { - add(sortedList, last(sortedList, &*error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), &*error); + add(sortedList, last(sortedList, error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); if (*error == 3) { return NULL; } - removeFirstElement(secondList, &*error); + removeFirstElement(secondList, error); if (*error == 2) { return NULL; @@ -82,26 +82,18 @@ List* mergeSort(List* list, int number, int* error) { return list; } - const int leftBorderOfList = numberOfElements(list, &*error) / 2; - if (*error == 3) - { - return NULL; - } - const int rightBorderOfList = numberOfElements(list, &*error); - if (*error == 3) - { - return NULL; - } + const int leftBorderOfList = numberOfElements(list) / 2; + const int rightBorderOfList = numberOfElements(list); List* leftHalfOfTheList = createList(); List* rightHalfOfTheList = createList(); for (int i = 1; i <= leftBorderOfList; i++) { - add(leftHalfOfTheList, last(leftHalfOfTheList, &*error), getHeadFirstValue(list), getHeadSecondValue(list), &*error); + add(leftHalfOfTheList, last(leftHalfOfTheList, error), getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { return NULL; } - removeFirstElement(list, &*error); + removeFirstElement(list, error); if (*error == 2) { return NULL; @@ -109,21 +101,21 @@ List* mergeSort(List* list, int number, int* error) } for (int i = leftBorderOfList + 1; i <= rightBorderOfList; i++) { - add(rightHalfOfTheList, last(rightHalfOfTheList, &*error), getHeadFirstValue(list), getHeadSecondValue(list), &*error); + add(rightHalfOfTheList, last(rightHalfOfTheList, error), getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { return NULL; } - removeFirstElement(list, &*error); + removeFirstElement(list, error); if (*error == 2) { return NULL; } } deleteList(list); - List* firstList = mergeSort(leftHalfOfTheList, number, &*error); - List* secondList = mergeSort(rightHalfOfTheList, number, &*error); - List* answer = merge(firstList, secondList, number, &*error); + List* firstList = mergeSort(leftHalfOfTheList, number, error); + List* secondList = mergeSort(rightHalfOfTheList, number, error); + List* answer = merge(firstList, secondList, number, error); deleteList(firstList); deleteList(secondList); return answer; diff --git a/NewList/NewList/MergeSort.h b/NewList/NewList/MergeSort.h index fa20f38..314865a 100644 --- a/NewList/NewList/MergeSort.h +++ b/NewList/NewList/MergeSort.h @@ -1,5 +1,4 @@ #pragma once -#include #include "List.h" // Function for merge sorting diff --git a/NewList/NewList/TestList.c b/NewList/NewList/TestList.c index 3288dad..b9b7c74 100644 --- a/NewList/NewList/TestList.c +++ b/NewList/NewList/TestList.c @@ -7,20 +7,24 @@ bool testAdd() { int error = 0; List* newList = createList(); - add(newList, last(newList, &error), (int*)"test", (int*)" 1", & error); - add(newList, last(newList, &error), (int*)"list", (int*)"add", & error); + add(newList, last(newList, &error), "test", " 1", &error); + add(newList, last(newList, &error), "list", "add", &error); if (error == 3) { deleteList(newList); return false; } - const int* firstStringFirstValue = newList->head->firstValue; - const int* firstStringSecondValue = newList->head->secondValue; - const int* secondStringFirstValue = newList->head->next->firstValue; - const int* secondStringSecondValue = newList->head->next->secondValue; + const char* firstStringFirstValue = getHeadFirstValue(newList); + const char* firstStringSecondValue = getHeadSecondValue(newList); + Position* position = first(newList, &error); + next(position); + const char* secondStringFirstValue = getFirstValue(position); + const char* secondStringSecondValue = getSecondValue(position); + bool result = strcmp(firstStringFirstValue, "test") == 0 && strcmp(firstStringSecondValue, " 1") == 0 + && strcmp(secondStringFirstValue, "list") == 0 && strcmp(secondStringSecondValue, "add") == 0; + deletePosition(position); deleteList(newList); - return strcmp((char*)firstStringFirstValue, "test") == 0 && strcmp((char*)firstStringSecondValue, " 1") == 0 - && strcmp((char*)secondStringFirstValue, "list") == 0 && strcmp((char*)secondStringSecondValue, "add") == 0; + return result; } // Function to check the function that deletes the first item in the list @@ -28,8 +32,8 @@ bool testRemoveHead() { int error = 0; List* newList = createList(); - add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); - add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); + add(newList, last(newList, &error), "Hello", "World", &error); + add(newList, last(newList, &error), "Merge", "Sort", &error); if (error == 3) { deleteList(newList); @@ -41,10 +45,11 @@ bool testRemoveHead() deleteList(newList); return false; } - const int* headOfListFirstValue = newList->head->firstValue; - const int* headOfListSecondValue = newList->head->secondValue; + const char* headOfListFirstValue = getHeadFirstValue(newList); + const char* headOfListSecondValue = getHeadSecondValue(newList); + bool result = strcmp(headOfListFirstValue, "Merge") == 0 && strcmp(headOfListSecondValue, "Sort") == 0; deleteList(newList); - return strcmp((char*)headOfListFirstValue, "Merge") == 0 && strcmp((char*)headOfListSecondValue, "Sort") == 0; + return result; } // Function for checking the function counting the number of elements @@ -52,25 +57,25 @@ bool testNumberOfElements() { List* newList = createList(); int error = 0; - add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); + add(newList, last(newList, &error), "Hello", "World", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); + add(newList, last(newList, &error), "Merge", "Sort", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), (int*)"test", (int*)" 1", &error); + add(newList, last(newList, &error), "test", " 1", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), (int*)"list", (int*)"add", &error); + add(newList, last(newList, &error), "list", "add", &error); if (error == 3) { deleteList(newList); @@ -91,25 +96,26 @@ bool testGetHeadFirstAndSecondValue() { List* newList = createList(); int error = 0; - add(newList, last(newList, &error), (int*)"Hello", (int*)"World", &error); + add(newList, last(newList, &error), "Hello", "World", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), (int*)"Merge", (int*)"Sort", &error); + add(newList, last(newList, &error), "Merge", "Sort", &error); if (error == 3) { deleteList(newList); return false; } - const int* firstHeadValue = getHeadFirstValue(newList); - const int* secondHeadValue = getHeadSecondValue(newList); + const char* firstHeadValue = getHeadFirstValue(newList); + const char* secondHeadValue = getHeadSecondValue(newList); + bool result = strcmp(firstHeadValue, "Hello") == 0 && strcmp(secondHeadValue, "World") == 0; deleteList(newList); - return strcmp((char*)firstHeadValue, "Hello") == 0 && strcmp((char*)secondHeadValue, "World") == 0; + return result; } bool allTest() { - return testAdd() && testNumberOfElements() && testRemoveHead() && testGetHeadFirstAndSecondValue(); + return testAdd() && testGetHeadFirstAndSecondValue() && testNumberOfElements() && testRemoveHead(); } \ No newline at end of file diff --git a/NewList/NewList/TestMergeSort.c b/NewList/NewList/TestMergeSort.c index 4c16286..6fb0eba 100644 --- a/NewList/NewList/TestMergeSort.c +++ b/NewList/NewList/TestMergeSort.c @@ -7,25 +7,25 @@ bool testMergeSort() { List* newFirstList = createList(); int error = 0; - add(newFirstList, last(newFirstList, &error), (int*)"Pavel", (int*)"890345", &error); + add(newFirstList, last(newFirstList, &error), "Pavel", "890345", &error); if (error == 3) { deleteList(newFirstList); return false; } - add(newFirstList, last(newFirstList, &error), (int*)"Alex", (int*)"765494", &error); + add(newFirstList, last(newFirstList, &error), "Alex", "765494", &error); if (error == 3) { deleteList(newFirstList); return false; } - add(newFirstList, last(newFirstList, &error), (int*)"Artem", (int*)"956469", &error); + add(newFirstList, last(newFirstList, &error), "Artem", "956469", &error); if (error == 3) { deleteList(newFirstList); return false; } - add(newFirstList, last(newFirstList, &error), (int*)"Ruslan", (int*)"2342424", &error); + add(newFirstList, last(newFirstList, &error), "Ruslan", "2342424", &error); if (error == 3) { deleteList(newFirstList); @@ -33,28 +33,28 @@ bool testMergeSort() } List* newSecondList = createList(); - add(newSecondList, last(newSecondList, &error), (int*)"Pavel", (int*)"890345", &error); + add(newSecondList, last(newSecondList, &error), "Pavel", "890345", &error); if (error == 3) { deleteList(newSecondList); deleteList(newFirstList); return false; } - add(newSecondList, last(newSecondList, &error), (int*)"Alex", (int*)"765494", &error); + add(newSecondList, last(newSecondList, &error), "Alex", "765494", &error); if (error == 3) { deleteList(newSecondList); deleteList(newFirstList); return false; } - add(newSecondList, last(newSecondList, &error), (int*)"Artem", (int*)"956469", &error); + add(newSecondList, last(newSecondList, &error), "Artem", "956469", &error); if (error == 3) { deleteList(newSecondList); deleteList(newFirstList); return false; } - add(newSecondList, last(newSecondList, &error), (int*)"Ruslan", (int*)"2342424", &error); + add(newSecondList, last(newSecondList, &error), "Ruslan", "2342424", &error); if (error == 3) { deleteList(newSecondList); @@ -64,44 +64,50 @@ bool testMergeSort() List* firstList = mergeSort(newFirstList, 1, &error); - const char* firstStringFirstValueFirstList = (char*)firstList->head->firstValue; - const char* firstStringSecondValueFirstList = (char*)firstList->head->secondValue; - - const char* secondStringFirstValueFirstList = (char*)firstList->head->next->firstValue; - const char* secondStringSecondValueFirstList = (char*)firstList->head->next->secondValue; - - const char* thirdStringFirstValueFirstList = (char*)firstList->head->next->next->firstValue; - const char* thirdStringSecondValueFirstList = (char*)firstList->head->next->next->secondValue; - - const char* fourthStringFirstValueFirstList = (char*)firstList->head->next->next->next->firstValue; - const char* fourthStringSecondValueFirstList = (char*)firstList->head->next->next->next->secondValue; + Position* position = first(firstList, &error); + const char* firstStringFirstValueFirstList = getFirstValue(position); + const char* firstStringSecondValueFirstList = getSecondValue(position); + next(position); + const char* secondStringFirstValueFirstList = getFirstValue(position); + const char* secondStringSecondValueFirstList = getSecondValue(position); + next(position); + const char* thirdStringFirstValueFirstList = getFirstValue(position); + const char* thirdStringSecondValueFirstList = getSecondValue(position); + next(position); + const char* fourthStringFirstValueFirstList = getFirstValue(position); + const char* fourthStringSecondValueFirstList = getSecondValue(position); + + deletePosition(position); List* secondList = mergeSort(newSecondList, 0, &error); - const char* firstStringFirstValueSecondList = (char*)secondList->head->firstValue; - const char* firstStringSecondValueSecondList = (char*)secondList->head->secondValue; - - const char* secondStringFirstValueSecondList = (char*)secondList->head->next->firstValue; - const char* secondStringSecondValueSecondList = (char*)secondList->head->next->secondValue; + Position* newPosition = first(secondList, &error); + const char* firstStringFirstValueSecondList = getFirstValue(newPosition); + const char* firstStringSecondValueSecondList = getSecondValue(newPosition); + next(newPosition); + const char* secondStringFirstValueSecondList = getFirstValue(newPosition); + const char* secondStringSecondValueSecondList = getSecondValue(newPosition); + next(newPosition); + const char* thirdStringFirstValueSecondList = getFirstValue(newPosition); + const char* thirdStringSecondValueSecondList = getSecondValue(newPosition); + next(newPosition); + const char* fourthStringFirstValueSecondList = getFirstValue(newPosition); + const char* fourthStringSecondValueSecondList = getSecondValue(newPosition); + + bool result = strcmp(firstStringFirstValueFirstList, "Alex") == 0 && strcmp(firstStringSecondValueFirstList, "765494") == 0 + && strcmp(secondStringFirstValueFirstList, "Artem") == 0 && strcmp(secondStringSecondValueFirstList, "956469") == 0 + && strcmp(thirdStringFirstValueFirstList, "Pavel") == 0 && strcmp(thirdStringSecondValueFirstList, "890345") == 0 + && strcmp(fourthStringFirstValueFirstList, "Ruslan") == 0 && strcmp(fourthStringSecondValueFirstList, "2342424") == 0 - const char* thirdStringFirstValueSecondList = (char*)secondList->head->next->next->firstValue; - const char* thirdStringSecondValueSecondList = (char*)secondList->head->next->next->secondValue; - - const char* fourthStringFirstValueSecondList = (char*)secondList->head->next->next->next->firstValue; - const char* fourthStringSecondValueSecondList = (char*)secondList->head->next->next->next->secondValue; + && strcmp(firstStringFirstValueSecondList, "Ruslan") == 0 && strcmp(firstStringSecondValueSecondList, "2342424") == 0 + && strcmp(secondStringFirstValueSecondList, "Alex") == 0 && strcmp(secondStringSecondValueSecondList, "765494") == 0 + && strcmp(thirdStringFirstValueSecondList, "Pavel") == 0 && strcmp(thirdStringSecondValueSecondList, "890345") == 0 + && strcmp(fourthStringFirstValueSecondList, "Artem") == 0 && strcmp(fourthStringSecondValueSecondList, "956469") == 0; + deletePosition(newPosition); deleteList(newFirstList); deleteList(newSecondList); deleteList(firstList); deleteList(secondList); - - return strcmp(firstStringFirstValueFirstList, "Alex") == 0 && strcmp(firstStringSecondValueFirstList, "765494") == 0 - && strcmp(secondStringFirstValueFirstList, "Artem") == 0 && strcmp(secondStringSecondValueFirstList, "956469") == 0 - && strcmp(thirdStringFirstValueFirstList, "Pavel") == 0 && strcmp(thirdStringSecondValueFirstList, "890345") == 0 - && strcmp(fourthStringFirstValueFirstList, "Ruslan") == 0 && strcmp(fourthStringSecondValueFirstList, "2342424") == 0 - - && strcmp(firstStringFirstValueSecondList, "Ruslan") == 0 && strcmp(firstStringSecondValueSecondList, "2342424") == 0 - && strcmp(secondStringFirstValueSecondList, "Alex") == 0 && strcmp(secondStringSecondValueSecondList, "765494") == 0 - && strcmp(thirdStringFirstValueSecondList, "Pavel") == 0 && strcmp(thirdStringSecondValueSecondList, "890345") == 0 - && strcmp(fourthStringFirstValueSecondList, "Artem") == 0 && strcmp(fourthStringSecondValueSecondList, "956469") == 0; + return result; } \ No newline at end of file From 39595a9cf232869d09dd4211c402d083c48090c5 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 20:38:43 +0500 Subject: [PATCH 05/24] change --- NewList/NewList/List.c | 17 ++++------------- NewList/NewList/List.h | 9 +++++---- NewList/NewList/Main.c | 4 +++- NewList/NewList/MergeSort.c | 18 ++++++++++++++++++ NewList/NewList/TestList.c | 7 +------ NewList/NewList/TestMergeSort.c | 1 - 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c index 0bf76c2..3fd0dc0 100644 --- a/NewList/NewList/List.c +++ b/NewList/NewList/List.c @@ -13,7 +13,7 @@ typedef struct List struct ListElement* tail; } List; -// A structure containing a pointer to the next list item and a value variable for the list items +// Structure containing a pointer to the next list item and a value variable for the list items typedef struct ListElement { char* firstValue; @@ -21,7 +21,7 @@ typedef struct ListElement struct ListElement* next; } ListElement; -// contains a pointer to a ListElement +// Structure containing a pointer to a ListElement typedef struct Position { ListElement* position; @@ -34,12 +34,6 @@ List* createList() void deleteList(List* list) { - if (list->head == NULL) - { - list->size = 0; - free(list); - return; - } ListElement* position = list->head; while (position != NULL) { @@ -89,7 +83,6 @@ Position* first(List* list, int* error) Position* last(List* list, int* error) { - *error = 0; Position* positionLast = malloc(sizeof(Position)); if (positionLast == NULL) { @@ -172,19 +165,17 @@ void add(List* list, Position* position, char* firstValue, char* secondValue, in ListElement* newElement = calloc(1, sizeof(ListElement)); if (newElement == NULL) { - free(firstValueCopy); - free(secondValueCopy); *error = 3; return; } newElement->firstValue = firstValueCopy; newElement->secondValue = secondValueCopy; - if (list->head == NULL) + if (position->position == NULL) { list->size = 1; list->head = newElement; list->tail = newElement; - list->tail = list->head; + position->position = newElement; return; } list->size++; diff --git a/NewList/NewList/List.h b/NewList/NewList/List.h index 85a961e..8c2a14e 100644 --- a/NewList/NewList/List.h +++ b/NewList/NewList/List.h @@ -13,17 +13,16 @@ List* createList(); // Function for deleting a list void deleteList(List* list); -// Function for deleting a position +// Function for freeing up memory void deletePosition(Position* position); // Function for adding an item to a list void add(List* list, Position* position, char* firstValue, char* secondValue , int* error); // function to find a pointer to the first element - Position* first(List* list, int* error); -// Function for moving to the next position +// Function for finding a pointer to the next element Position* next(Position* position); // Function for checking an item for being at the end of the list @@ -50,9 +49,11 @@ char* getHeadFirstValue(List* list); // Function that returns a value for the first element char* getHeadSecondValue(List* list); -// Function for finding the position of the last element +// Function for finding a pointer to the last element Position* last(List* list, int* error); +// Returns the value of any element char* getFirstValue(Position* position); +// Returns the value of any element char* getSecondValue(Position* position); diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c index d547b64..82bb0af 100644 --- a/NewList/NewList/Main.c +++ b/NewList/NewList/Main.c @@ -25,12 +25,14 @@ void readPhonebook(List* list, const char* fileName, int* error) break; } } - add(list, last(list, error), arrayForName, arrayForNumber, error); + Position* position = last(list, error); + add(list, position, arrayForName, arrayForNumber, error); if (*error == 3) { fclose(file); return; } + free(position); } fclose(file); } diff --git a/NewList/NewList/MergeSort.c b/NewList/NewList/MergeSort.c index a99c489..8b6fded 100644 --- a/NewList/NewList/MergeSort.c +++ b/NewList/NewList/MergeSort.c @@ -23,11 +23,15 @@ List* merge(List* firstList, List* secondList, int number, int* error) add(sortedList, last(sortedList, error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { + deleteList(firstList); + deleteList(secondList); return NULL; } removeFirstElement(firstList, error); if (*error == 2) { + deleteList(firstList); + deleteList(secondList); return NULL; } } @@ -37,6 +41,8 @@ List* merge(List* firstList, List* secondList, int number, int* error) removeFirstElement(secondList, error); if (*error == 2) { + deleteList(firstList); + deleteList(secondList); return NULL; } } @@ -46,11 +52,15 @@ List* merge(List* firstList, List* secondList, int number, int* error) add(sortedList, last(sortedList, error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { + deleteList(firstList); + deleteList(secondList); return NULL; } removeFirstElement(firstList, error); if (*error == 2) { + deleteList(firstList); + deleteList(secondList); return NULL; } } @@ -59,11 +69,15 @@ List* merge(List* firstList, List* secondList, int number, int* error) add(sortedList, last(sortedList, error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); if (*error == 3) { + deleteList(firstList); + deleteList(secondList); return NULL; } removeFirstElement(secondList, error); if (*error == 2) { + deleteList(firstList); + deleteList(secondList); return NULL; } } @@ -91,11 +105,13 @@ List* mergeSort(List* list, int number, int* error) add(leftHalfOfTheList, last(leftHalfOfTheList, error), getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { + deleteList(list); return NULL; } removeFirstElement(list, error); if (*error == 2) { + deleteList(list); return NULL; } } @@ -104,11 +120,13 @@ List* mergeSort(List* list, int number, int* error) add(rightHalfOfTheList, last(rightHalfOfTheList, error), getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { + deleteList(list); return NULL; } removeFirstElement(list, error); if (*error == 2) { + deleteList(list); return NULL; } } diff --git a/NewList/NewList/TestList.c b/NewList/NewList/TestList.c index b9b7c74..f82a52f 100644 --- a/NewList/NewList/TestList.c +++ b/NewList/NewList/TestList.c @@ -81,12 +81,7 @@ bool testNumberOfElements() deleteList(newList); return false; } - const int number = numberOfElements(newList, &error); - if (error == 3) - { - deleteList(newList); - return false; - } + const int number = numberOfElements(newList); deleteList(newList); return number == 4; } diff --git a/NewList/NewList/TestMergeSort.c b/NewList/NewList/TestMergeSort.c index 6fb0eba..1ab2e64 100644 --- a/NewList/NewList/TestMergeSort.c +++ b/NewList/NewList/TestMergeSort.c @@ -31,7 +31,6 @@ bool testMergeSort() deleteList(newFirstList); return false; } - List* newSecondList = createList(); add(newSecondList, last(newSecondList, &error), "Pavel", "890345", &error); if (error == 3) From cf166c355660102f5c6cee76d561440d9952cd7f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 7 Nov 2021 21:31:36 +0500 Subject: [PATCH 06/24] changing tests --- NewList/NewList/List.c | 37 +++++++++++++----- NewList/NewList/List.h | 13 ++++--- NewList/NewList/Main.c | 4 +- NewList/NewList/MergeSort.c | 12 +++--- NewList/NewList/TestList.c | 20 +++++----- NewList/NewList/TestMergeSort.c | 67 ++++++--------------------------- 6 files changed, 64 insertions(+), 89 deletions(-) diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c index 3fd0dc0..bb30aec 100644 --- a/NewList/NewList/List.c +++ b/NewList/NewList/List.c @@ -4,7 +4,6 @@ #include #include - // Structure containing pointers to the beginning and end of the list typedef struct List { @@ -145,7 +144,7 @@ char* getSecondValue(Position* position) return position->position->secondValue; } -void add(List* list, Position* position, char* firstValue, char* secondValue, int* error) +void add(List* list, char* firstValue, char* secondValue, int* error) { char* firstValueCopy = calloc(100, sizeof(char)); if (firstValueCopy == NULL) @@ -170,16 +169,15 @@ void add(List* list, Position* position, char* firstValue, char* secondValue, in } newElement->firstValue = firstValueCopy; newElement->secondValue = secondValueCopy; - if (position->position == NULL) + if (list->head == NULL) { list->size = 1; list->head = newElement; list->tail = newElement; - position->position = newElement; return; } list->size++; - position->position->next = newElement; + list->tail->next = newElement; list->tail = list->tail->next; } @@ -196,10 +194,31 @@ bool isOneElement(List* list) void print(List* list) { int error = 0; - for (Position* position = first(list, &error); !isLastElement(position); position = next(position)) + ListElement* element = list->head; + while (element != NULL) + { + printf("%s ", element->firstValue); + printf("%s\n", element->secondValue); + element = element->next; + } +} + +bool compareList(List* firstList, List* secondList) +{ + ListElement* firstElement = firstList->head; + ListElement* secondElement = secondList->head; + if (numberOfElements(firstList) != numberOfElements(secondList)) + { + return false; + } + while (firstElement != NULL) { - printf("%s - ", position->position->firstValue); - printf("%s", position->position->secondValue); - printf("\n"); + if (strcmp(firstElement->firstValue, firstElement->secondValue) != 0 || strcmp(firstElement->firstValue, firstElement->secondValue) != 0) + { + return true; + } + firstElement = firstElement->next; + secondElement = secondElement->next; } + return true; } \ No newline at end of file diff --git a/NewList/NewList/List.h b/NewList/NewList/List.h index 8c2a14e..05c488b 100644 --- a/NewList/NewList/List.h +++ b/NewList/NewList/List.h @@ -4,7 +4,7 @@ // Structure that represents list typedef struct List List; -// Structure that represents list +// This is a structure describing the position of an item in the list. typedef struct Position Position; // Function for creating a list @@ -17,12 +17,12 @@ void deleteList(List* list); void deletePosition(Position* position); // Function for adding an item to a list -void add(List* list, Position* position, char* firstValue, char* secondValue , int* error); +void add(List* list, char* firstValue, char* secondValue , int* error); -// function to find a pointer to the first element +// function to find a position to the first element Position* first(List* list, int* error); -// Function for finding a pointer to the next element +// Function for finding a position to the next element Position* next(Position* position); // Function for checking an item for being at the end of the list @@ -49,7 +49,7 @@ char* getHeadFirstValue(List* list); // Function that returns a value for the first element char* getHeadSecondValue(List* list); -// Function for finding a pointer to the last element +// Function for finding a position to the last element Position* last(List* list, int* error); // Returns the value of any element @@ -57,3 +57,6 @@ char* getFirstValue(Position* position); // Returns the value of any element char* getSecondValue(Position* position); + +// Function for comparing lists +bool compareList(List* firstList, List* secondList); \ No newline at end of file diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c index 82bb0af..0d9ffe7 100644 --- a/NewList/NewList/Main.c +++ b/NewList/NewList/Main.c @@ -25,14 +25,12 @@ void readPhonebook(List* list, const char* fileName, int* error) break; } } - Position* position = last(list, error); - add(list, position, arrayForName, arrayForNumber, error); + add(list, arrayForName, arrayForNumber, error); if (*error == 3) { fclose(file); return; } - free(position); } fclose(file); } diff --git a/NewList/NewList/MergeSort.c b/NewList/NewList/MergeSort.c index 8b6fded..ee42c40 100644 --- a/NewList/NewList/MergeSort.c +++ b/NewList/NewList/MergeSort.c @@ -20,7 +20,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) { if (compareTheHeads(firstList, secondList, number)) { - add(sortedList, last(sortedList, error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); + add(sortedList, getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { deleteList(firstList); @@ -37,7 +37,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) } else { - add(sortedList, last(sortedList, error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); + add(sortedList, getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); removeFirstElement(secondList, error); if (*error == 2) { @@ -49,7 +49,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) } while (!isEmpty(firstList)) { - add(sortedList, last(sortedList, error), getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); + add(sortedList, getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { deleteList(firstList); @@ -66,7 +66,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) } while (!isEmpty(secondList)) { - add(sortedList, last(sortedList, error), getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); + add(sortedList, getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); if (*error == 3) { deleteList(firstList); @@ -102,7 +102,7 @@ List* mergeSort(List* list, int number, int* error) List* rightHalfOfTheList = createList(); for (int i = 1; i <= leftBorderOfList; i++) { - add(leftHalfOfTheList, last(leftHalfOfTheList, error), getHeadFirstValue(list), getHeadSecondValue(list), error); + add(leftHalfOfTheList, getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { deleteList(list); @@ -117,7 +117,7 @@ List* mergeSort(List* list, int number, int* error) } for (int i = leftBorderOfList + 1; i <= rightBorderOfList; i++) { - add(rightHalfOfTheList, last(rightHalfOfTheList, error), getHeadFirstValue(list), getHeadSecondValue(list), error); + add(rightHalfOfTheList, getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { deleteList(list); diff --git a/NewList/NewList/TestList.c b/NewList/NewList/TestList.c index f82a52f..615c82c 100644 --- a/NewList/NewList/TestList.c +++ b/NewList/NewList/TestList.c @@ -7,8 +7,8 @@ bool testAdd() { int error = 0; List* newList = createList(); - add(newList, last(newList, &error), "test", " 1", &error); - add(newList, last(newList, &error), "list", "add", &error); + add(newList, "test", " 1", &error); + add(newList, "list", "add", &error); if (error == 3) { deleteList(newList); @@ -32,8 +32,8 @@ bool testRemoveHead() { int error = 0; List* newList = createList(); - add(newList, last(newList, &error), "Hello", "World", &error); - add(newList, last(newList, &error), "Merge", "Sort", &error); + add(newList, "Hello", "World", &error); + add(newList, "Merge", "Sort", &error); if (error == 3) { deleteList(newList); @@ -57,25 +57,25 @@ bool testNumberOfElements() { List* newList = createList(); int error = 0; - add(newList, last(newList, &error), "Hello", "World", &error); + add(newList, "Hello", "World", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), "Merge", "Sort", &error); + add(newList, "Merge", "Sort", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), "test", " 1", &error); + add(newList, "test", " 1", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), "list", "add", &error); + add(newList, "list", "add", &error); if (error == 3) { deleteList(newList); @@ -91,13 +91,13 @@ bool testGetHeadFirstAndSecondValue() { List* newList = createList(); int error = 0; - add(newList, last(newList, &error), "Hello", "World", &error); + add(newList, "Hello", "World", &error); if (error == 3) { deleteList(newList); return false; } - add(newList, last(newList, &error), "Merge", "Sort", &error); + add(newList, "Merge", "Sort", &error); if (error == 3) { deleteList(newList); diff --git a/NewList/NewList/TestMergeSort.c b/NewList/NewList/TestMergeSort.c index 1ab2e64..11fc5e1 100644 --- a/NewList/NewList/TestMergeSort.c +++ b/NewList/NewList/TestMergeSort.c @@ -7,106 +7,61 @@ bool testMergeSort() { List* newFirstList = createList(); int error = 0; - add(newFirstList, last(newFirstList, &error), "Pavel", "890345", &error); + add(newFirstList, "Pavel", "890345", &error); if (error == 3) { deleteList(newFirstList); return false; } - add(newFirstList, last(newFirstList, &error), "Alex", "765494", &error); + add(newFirstList, "Alex", "765494", &error); if (error == 3) { deleteList(newFirstList); return false; } - add(newFirstList, last(newFirstList, &error), "Artem", "956469", &error); + add(newFirstList, "Artem", "956469", &error); if (error == 3) { deleteList(newFirstList); return false; } - add(newFirstList, last(newFirstList, &error), "Ruslan", "2342424", &error); + add(newFirstList, "Ruslan", "2342424", &error); if (error == 3) { deleteList(newFirstList); return false; } List* newSecondList = createList(); - add(newSecondList, last(newSecondList, &error), "Pavel", "890345", &error); + add(newSecondList, "Alex", "765494", &error); if (error == 3) { - deleteList(newSecondList); deleteList(newFirstList); return false; } - add(newSecondList, last(newSecondList, &error), "Alex", "765494", &error); + add(newSecondList, "Artem", "956469", &error); if (error == 3) { - deleteList(newSecondList); deleteList(newFirstList); return false; } - add(newSecondList, last(newSecondList, &error), "Artem", "956469", &error); + add(newSecondList, "Pavel", "890345", &error); if (error == 3) { - deleteList(newSecondList); deleteList(newFirstList); return false; } - add(newSecondList, last(newSecondList, &error), "Ruslan", "2342424", &error); + add(newSecondList, "Ruslan", "2342424", &error); if (error == 3) { - deleteList(newSecondList); deleteList(newFirstList); return false; } - List* firstList = mergeSort(newFirstList, 1, &error); - - Position* position = first(firstList, &error); - const char* firstStringFirstValueFirstList = getFirstValue(position); - const char* firstStringSecondValueFirstList = getSecondValue(position); - next(position); - const char* secondStringFirstValueFirstList = getFirstValue(position); - const char* secondStringSecondValueFirstList = getSecondValue(position); - next(position); - const char* thirdStringFirstValueFirstList = getFirstValue(position); - const char* thirdStringSecondValueFirstList = getSecondValue(position); - next(position); - const char* fourthStringFirstValueFirstList = getFirstValue(position); - const char* fourthStringSecondValueFirstList = getSecondValue(position); - - deletePosition(position); - - List* secondList = mergeSort(newSecondList, 0, &error); - - Position* newPosition = first(secondList, &error); - const char* firstStringFirstValueSecondList = getFirstValue(newPosition); - const char* firstStringSecondValueSecondList = getSecondValue(newPosition); - next(newPosition); - const char* secondStringFirstValueSecondList = getFirstValue(newPosition); - const char* secondStringSecondValueSecondList = getSecondValue(newPosition); - next(newPosition); - const char* thirdStringFirstValueSecondList = getFirstValue(newPosition); - const char* thirdStringSecondValueSecondList = getSecondValue(newPosition); - next(newPosition); - const char* fourthStringFirstValueSecondList = getFirstValue(newPosition); - const char* fourthStringSecondValueSecondList = getSecondValue(newPosition); - - bool result = strcmp(firstStringFirstValueFirstList, "Alex") == 0 && strcmp(firstStringSecondValueFirstList, "765494") == 0 - && strcmp(secondStringFirstValueFirstList, "Artem") == 0 && strcmp(secondStringSecondValueFirstList, "956469") == 0 - && strcmp(thirdStringFirstValueFirstList, "Pavel") == 0 && strcmp(thirdStringSecondValueFirstList, "890345") == 0 - && strcmp(fourthStringFirstValueFirstList, "Ruslan") == 0 && strcmp(fourthStringSecondValueFirstList, "2342424") == 0 - - && strcmp(firstStringFirstValueSecondList, "Ruslan") == 0 && strcmp(firstStringSecondValueSecondList, "2342424") == 0 - && strcmp(secondStringFirstValueSecondList, "Alex") == 0 && strcmp(secondStringSecondValueSecondList, "765494") == 0 - && strcmp(thirdStringFirstValueSecondList, "Pavel") == 0 && strcmp(thirdStringSecondValueSecondList, "890345") == 0 - && strcmp(fourthStringFirstValueSecondList, "Artem") == 0 && strcmp(fourthStringSecondValueSecondList, "956469") == 0; - - deletePosition(newPosition); + print(firstList); + print(newSecondList); + bool result = compareList(firstList, newSecondList); deleteList(newFirstList); deleteList(newSecondList); deleteList(firstList); - deleteList(secondList); return result; } \ No newline at end of file From 898009cf34c2b341cacd3386553f36a8c98fc197 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Tue, 9 Nov 2021 17:02:38 +0500 Subject: [PATCH 07/24] fixed some memory leaks --- NewList/NewList/List.c | 20 +++++++++++++++----- NewList/NewList/Main.c | 3 +-- NewList/NewList/MergeSort.c | 20 +++++++++++++++----- NewList/NewList/Phonebook.txt | 2 +- NewList/NewList/TestMergeSort.c | 7 ++++--- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c index bb30aec..6f8fae8 100644 --- a/NewList/NewList/List.c +++ b/NewList/NewList/List.c @@ -42,7 +42,6 @@ void deleteList(List* list) free(position); position = list->head; } - list->size = 0; free(list); } @@ -59,12 +58,23 @@ void removeFirstElement(List* list, int* error) *error = 2; return; } - ListElement* position = list->head; + if (list->head == list->tail) + { + list->size = 0; + free(list->head->secondValue); + free(list->head->firstValue); + free(list->head); + list->head = NULL; + list->tail = NULL; + return; + } + ListElement* element = list->head; list->head = list->head->next; list->size--; - free(position->secondValue); - free(position->firstValue); - free(position); + free(element->secondValue); + free(element->firstValue); + free(element); + } Position* first(List* list, int* error) diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c index 0d9ffe7..0dc95d1 100644 --- a/NewList/NewList/Main.c +++ b/NewList/NewList/Main.c @@ -4,6 +4,7 @@ #include "TestMergeSort.h" #include #include +#include void readPhonebook(List* list, const char* fileName, int* error) { @@ -37,7 +38,6 @@ void readPhonebook(List* list, const char* fileName, int* error) int main() { - setlocale(LC_ALL, "rus"); if (!allTest() || !testMergeSort()) { printf(" "); @@ -56,7 +56,6 @@ int main() return -1; } List* answer = mergeSort(newList, number, &error); - deleteList(newList); if (error == 1) { deleteList(answer); diff --git a/NewList/NewList/MergeSort.c b/NewList/NewList/MergeSort.c index ee42c40..81a7154 100644 --- a/NewList/NewList/MergeSort.c +++ b/NewList/NewList/MergeSort.c @@ -23,6 +23,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) add(sortedList, getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { + deleteList(sortedList); deleteList(firstList); deleteList(secondList); return NULL; @@ -30,6 +31,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) removeFirstElement(firstList, error); if (*error == 2) { + deleteList(sortedList); deleteList(firstList); deleteList(secondList); return NULL; @@ -41,6 +43,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) removeFirstElement(secondList, error); if (*error == 2) { + deleteList(sortedList); deleteList(firstList); deleteList(secondList); return NULL; @@ -52,6 +55,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) add(sortedList, getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); if (*error == 3) { + deleteList(sortedList); deleteList(firstList); deleteList(secondList); return NULL; @@ -69,6 +73,7 @@ List* merge(List* firstList, List* secondList, int number, int* error) add(sortedList, getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); if (*error == 3) { + deleteList(sortedList); deleteList(firstList); deleteList(secondList); return NULL; @@ -76,13 +81,12 @@ List* merge(List* firstList, List* secondList, int number, int* error) removeFirstElement(secondList, error); if (*error == 2) { + deleteList(sortedList); deleteList(firstList); deleteList(secondList); return NULL; } } - deleteList(firstList); - deleteList(secondList); return sortedList; } @@ -105,12 +109,16 @@ List* mergeSort(List* list, int number, int* error) add(leftHalfOfTheList, getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { + deleteList(leftHalfOfTheList); + deleteList(rightHalfOfTheList); deleteList(list); return NULL; } removeFirstElement(list, error); if (*error == 2) { + deleteList(leftHalfOfTheList); + deleteList(rightHalfOfTheList); deleteList(list); return NULL; } @@ -120,21 +128,23 @@ List* mergeSort(List* list, int number, int* error) add(rightHalfOfTheList, getHeadFirstValue(list), getHeadSecondValue(list), error); if (*error == 3) { + deleteList(leftHalfOfTheList); + deleteList(rightHalfOfTheList); deleteList(list); return NULL; } removeFirstElement(list, error); if (*error == 2) { + deleteList(leftHalfOfTheList); + deleteList(rightHalfOfTheList); deleteList(list); return NULL; } } - deleteList(list); + free(list); List* firstList = mergeSort(leftHalfOfTheList, number, error); List* secondList = mergeSort(rightHalfOfTheList, number, error); List* answer = merge(firstList, secondList, number, error); - deleteList(firstList); - deleteList(secondList); return answer; } \ No newline at end of file diff --git a/NewList/NewList/Phonebook.txt b/NewList/NewList/Phonebook.txt index 8db74ad..cbba9f9 100644 --- a/NewList/NewList/Phonebook.txt +++ b/NewList/NewList/Phonebook.txt @@ -2,4 +2,4 @@ Pavel 89072 Sasha 21312 Artem 62513 Ruslan 41211 -Nikolay 123123 \ No newline at end of file +Nikolay 22415 \ No newline at end of file diff --git a/NewList/NewList/TestMergeSort.c b/NewList/NewList/TestMergeSort.c index 11fc5e1..d73b821 100644 --- a/NewList/NewList/TestMergeSort.c +++ b/NewList/NewList/TestMergeSort.c @@ -36,31 +36,32 @@ bool testMergeSort() if (error == 3) { deleteList(newFirstList); + deleteList(newSecondList); return false; } add(newSecondList, "Artem", "956469", &error); if (error == 3) { deleteList(newFirstList); + deleteList(newSecondList); return false; } add(newSecondList, "Pavel", "890345", &error); if (error == 3) { deleteList(newFirstList); + deleteList(newSecondList); return false; } add(newSecondList, "Ruslan", "2342424", &error); if (error == 3) { deleteList(newFirstList); + deleteList(newSecondList); return false; } List* firstList = mergeSort(newFirstList, 1, &error); - print(firstList); - print(newSecondList); bool result = compareList(firstList, newSecondList); - deleteList(newFirstList); deleteList(newSecondList); deleteList(firstList); return result; From ec14372fe7c419ad1360b42bb87b1c89f2495ca2 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 10 Nov 2021 17:32:07 +0500 Subject: [PATCH 08/24] changing the project structure --- NewList/NewList/Main.c | 73 +------------------------------------ NewList/NewList/MergeSort.c | 3 ++ 2 files changed, 5 insertions(+), 71 deletions(-) diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c index 0dc95d1..4461872 100644 --- a/NewList/NewList/Main.c +++ b/NewList/NewList/Main.c @@ -1,80 +1,11 @@ -#include "List.h" -#include "MergeSort.h" #include "TestList.h" -#include "TestMergeSort.h" #include -#include -#include - -void readPhonebook(List* list, const char* fileName, int* error) -{ - FILE* file = fopen(fileName, "r"); - if (file == NULL) - { - *error = 1; - return; - } - while (!feof(file)) - { - char arrayForName[100] = {'\0'}; - char arrayForNumber[100] = {'\0'}; - if (fscanf(file, "%s", arrayForName) != EOF); - { - const int fscanfResult = fscanf(file, "%s", arrayForNumber); - if (fscanfResult == 0) - { - break; - } - } - add(list, arrayForName, arrayForNumber, error); - if (*error == 3) - { - fclose(file); - return; - } - } - fclose(file); -} int main() { - if (!allTest() || !testMergeSort()) - { - printf(" "); - return -1; - } - List* newList = createList(); - int error = 0; - readPhonebook(newList, "Phonebook.txt", &error); - printf(" ?\n"); - printf(" 1 , \n"); - int number = 0; - const int scanResult = scanf("%d", &number); - if (scanResult == 0) - { - printf(" "); - return -1; - } - List* answer = mergeSort(newList, number, &error); - if (error == 1) - { - deleteList(answer); - printf(" "); - return -1; - } - if (error == 2) - { - deleteList(answer); - printf(" "); - return -1; - } - if (error == 3) + if (!allTest()) { - deleteList(answer); - printf(" "); + printf("Test failed"); return -1; } - print(answer); - printf("\n"); - deleteList(answer); } \ No newline at end of file diff --git a/NewList/NewList/MergeSort.c b/NewList/NewList/MergeSort.c index 81a7154..ced1371 100644 --- a/NewList/NewList/MergeSort.c +++ b/NewList/NewList/MergeSort.c @@ -1,5 +1,6 @@ #include "MergeSort.h" #include +#include "stdlib.h" bool compareTheHeads(List* firstList, List* secondlist, int number) { @@ -87,6 +88,8 @@ List* merge(List* firstList, List* secondList, int number, int* error) return NULL; } } + free(firstList); + free(secondList); return sortedList; } From 70da8f8cc41388ec86834db75f1304a8876aaa7f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 10 Nov 2021 17:48:12 +0500 Subject: [PATCH 09/24] merge sorting is implemented in a separate project --- NewList/NewList/MergeSort.c | 153 ------------------------ NewList/NewList/MergeSort.h | 5 - NewList/NewList/NewList.vcxproj | 4 - NewList/NewList/NewList.vcxproj.filters | 12 -- NewList/NewList/Phonebook.txt | 5 - NewList/NewList/TestMergeSort.c | 68 ----------- NewList/NewList/TestMergeSort.h | 5 - 7 files changed, 252 deletions(-) delete mode 100644 NewList/NewList/MergeSort.c delete mode 100644 NewList/NewList/MergeSort.h delete mode 100644 NewList/NewList/Phonebook.txt delete mode 100644 NewList/NewList/TestMergeSort.c delete mode 100644 NewList/NewList/TestMergeSort.h diff --git a/NewList/NewList/MergeSort.c b/NewList/NewList/MergeSort.c deleted file mode 100644 index ced1371..0000000 --- a/NewList/NewList/MergeSort.c +++ /dev/null @@ -1,153 +0,0 @@ -#include "MergeSort.h" -#include -#include "stdlib.h" - -bool compareTheHeads(List* firstList, List* secondlist, int number) -{ - if (number == 1) - { - return strcmp(getHeadFirstValue(firstList), getHeadFirstValue(secondlist)) <= 0; - } - else - { - return strcmp(getHeadSecondValue(firstList), getHeadSecondValue(secondlist)) <= 0; - } -} - -List* merge(List* firstList, List* secondList, int number, int* error) -{ - List* sortedList = createList(); - while (!isEmpty(firstList) && !isEmpty(secondList)) - { - if (compareTheHeads(firstList, secondList, number)) - { - add(sortedList, getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); - if (*error == 3) - { - deleteList(sortedList); - deleteList(firstList); - deleteList(secondList); - return NULL; - } - removeFirstElement(firstList, error); - if (*error == 2) - { - deleteList(sortedList); - deleteList(firstList); - deleteList(secondList); - return NULL; - } - } - else - { - add(sortedList, getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); - removeFirstElement(secondList, error); - if (*error == 2) - { - deleteList(sortedList); - deleteList(firstList); - deleteList(secondList); - return NULL; - } - } - } - while (!isEmpty(firstList)) - { - add(sortedList, getHeadFirstValue(firstList), getHeadSecondValue(firstList), error); - if (*error == 3) - { - deleteList(sortedList); - deleteList(firstList); - deleteList(secondList); - return NULL; - } - removeFirstElement(firstList, error); - if (*error == 2) - { - deleteList(firstList); - deleteList(secondList); - return NULL; - } - } - while (!isEmpty(secondList)) - { - add(sortedList, getHeadFirstValue(secondList), getHeadSecondValue(secondList), error); - if (*error == 3) - { - deleteList(sortedList); - deleteList(firstList); - deleteList(secondList); - return NULL; - } - removeFirstElement(secondList, error); - if (*error == 2) - { - deleteList(sortedList); - deleteList(firstList); - deleteList(secondList); - return NULL; - } - } - free(firstList); - free(secondList); - return sortedList; -} - -List* mergeSort(List* list, int number, int* error) -{ - if (isEmpty(list)) - { - return list; - } - if (isOneElement(list)) - { - return list; - } - const int leftBorderOfList = numberOfElements(list) / 2; - const int rightBorderOfList = numberOfElements(list); - List* leftHalfOfTheList = createList(); - List* rightHalfOfTheList = createList(); - for (int i = 1; i <= leftBorderOfList; i++) - { - add(leftHalfOfTheList, getHeadFirstValue(list), getHeadSecondValue(list), error); - if (*error == 3) - { - deleteList(leftHalfOfTheList); - deleteList(rightHalfOfTheList); - deleteList(list); - return NULL; - } - removeFirstElement(list, error); - if (*error == 2) - { - deleteList(leftHalfOfTheList); - deleteList(rightHalfOfTheList); - deleteList(list); - return NULL; - } - } - for (int i = leftBorderOfList + 1; i <= rightBorderOfList; i++) - { - add(rightHalfOfTheList, getHeadFirstValue(list), getHeadSecondValue(list), error); - if (*error == 3) - { - deleteList(leftHalfOfTheList); - deleteList(rightHalfOfTheList); - deleteList(list); - return NULL; - } - removeFirstElement(list, error); - if (*error == 2) - { - deleteList(leftHalfOfTheList); - deleteList(rightHalfOfTheList); - deleteList(list); - return NULL; - } - } - free(list); - List* firstList = mergeSort(leftHalfOfTheList, number, error); - List* secondList = mergeSort(rightHalfOfTheList, number, error); - List* answer = merge(firstList, secondList, number, error); - return answer; -} \ No newline at end of file diff --git a/NewList/NewList/MergeSort.h b/NewList/NewList/MergeSort.h deleted file mode 100644 index 314865a..0000000 --- a/NewList/NewList/MergeSort.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include "List.h" - -// Function for merge sorting -List* mergeSort(List* list, int number, int* error); diff --git a/NewList/NewList/NewList.vcxproj b/NewList/NewList/NewList.vcxproj index 4fe7da0..286a851 100644 --- a/NewList/NewList/NewList.vcxproj +++ b/NewList/NewList/NewList.vcxproj @@ -141,15 +141,11 @@ - - - - diff --git a/NewList/NewList/NewList.vcxproj.filters b/NewList/NewList/NewList.vcxproj.filters index 2c57a68..230bb50 100644 --- a/NewList/NewList/NewList.vcxproj.filters +++ b/NewList/NewList/NewList.vcxproj.filters @@ -24,12 +24,6 @@ Исходные файлы - - Исходные файлы - - - Исходные файлы - @@ -38,11 +32,5 @@ Файлы заголовков - - Файлы заголовков - - - Файлы заголовков - \ No newline at end of file diff --git a/NewList/NewList/Phonebook.txt b/NewList/NewList/Phonebook.txt deleted file mode 100644 index cbba9f9..0000000 --- a/NewList/NewList/Phonebook.txt +++ /dev/null @@ -1,5 +0,0 @@ -Pavel 89072 -Sasha 21312 -Artem 62513 -Ruslan 41211 -Nikolay 22415 \ No newline at end of file diff --git a/NewList/NewList/TestMergeSort.c b/NewList/NewList/TestMergeSort.c deleted file mode 100644 index d73b821..0000000 --- a/NewList/NewList/TestMergeSort.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "TestMergeSort.h" -#include "MergeSort.h" -#include "List.h" -#include - -bool testMergeSort() -{ - List* newFirstList = createList(); - int error = 0; - add(newFirstList, "Pavel", "890345", &error); - if (error == 3) - { - deleteList(newFirstList); - return false; - } - add(newFirstList, "Alex", "765494", &error); - if (error == 3) - { - deleteList(newFirstList); - return false; - } - add(newFirstList, "Artem", "956469", &error); - if (error == 3) - { - deleteList(newFirstList); - return false; - } - add(newFirstList, "Ruslan", "2342424", &error); - if (error == 3) - { - deleteList(newFirstList); - return false; - } - List* newSecondList = createList(); - add(newSecondList, "Alex", "765494", &error); - if (error == 3) - { - deleteList(newFirstList); - deleteList(newSecondList); - return false; - } - add(newSecondList, "Artem", "956469", &error); - if (error == 3) - { - deleteList(newFirstList); - deleteList(newSecondList); - return false; - } - add(newSecondList, "Pavel", "890345", &error); - if (error == 3) - { - deleteList(newFirstList); - deleteList(newSecondList); - return false; - } - add(newSecondList, "Ruslan", "2342424", &error); - if (error == 3) - { - deleteList(newFirstList); - deleteList(newSecondList); - return false; - } - List* firstList = mergeSort(newFirstList, 1, &error); - bool result = compareList(firstList, newSecondList); - deleteList(newSecondList); - deleteList(firstList); - return result; -} \ No newline at end of file diff --git a/NewList/NewList/TestMergeSort.h b/NewList/NewList/TestMergeSort.h deleted file mode 100644 index 9ed03fc..0000000 --- a/NewList/NewList/TestMergeSort.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include - -// Function for checking the merge sort function -bool testMergeSort(); \ No newline at end of file From a92c7366b5508d9af306bba3f71b903163f75115 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 10 Nov 2021 18:02:23 +0500 Subject: [PATCH 10/24] fixed function for compare head --- NewList/NewList/List.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c index 6f8fae8..17e09fb 100644 --- a/NewList/NewList/List.c +++ b/NewList/NewList/List.c @@ -225,7 +225,7 @@ bool compareList(List* firstList, List* secondList) { if (strcmp(firstElement->firstValue, firstElement->secondValue) != 0 || strcmp(firstElement->firstValue, firstElement->secondValue) != 0) { - return true; + return false; } firstElement = firstElement->next; secondElement = secondElement->next; From 7d2d6e2a458f9179787667dc6b248a2cb861d35a Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 10 Nov 2021 18:07:20 +0500 Subject: [PATCH 11/24] fixed function for compare list --- NewList/NewList/List.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c index 17e09fb..c3dba4d 100644 --- a/NewList/NewList/List.c +++ b/NewList/NewList/List.c @@ -223,7 +223,7 @@ bool compareList(List* firstList, List* secondList) } while (firstElement != NULL) { - if (strcmp(firstElement->firstValue, firstElement->secondValue) != 0 || strcmp(firstElement->firstValue, firstElement->secondValue) != 0) + if (strcmp(firstElement->firstValue, secondElement->firstValue) != 0 || strcmp(firstElement->secondValue, secondElement->secondValue) != 0) { return false; } From 2f1bae8b58cdf9c12963465a4fad8a0859e97043 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Mon, 15 Nov 2021 21:57:45 +0300 Subject: [PATCH 12/24] the system of working with errors has been changed --- "Homework \342\204\2266/NewList/NewList.sln" | 31 +++ .../NewList/NewList/List.c" | 248 ++++++++++++++++++ .../NewList/NewList/List.h" | 70 +++++ .../NewList/NewList/Main.c" | 11 + .../NewList/NewList/NewList.vcxproj" | 153 +++++++++++ .../NewList/NewList/NewList.vcxproj.filters" | 36 +++ .../NewList/NewList/TestList.c" | 96 +++++++ .../NewList/NewList/TestList.h" | 5 + 8 files changed, 650 insertions(+) create mode 100644 "Homework \342\204\2266/NewList/NewList.sln" create mode 100644 "Homework \342\204\2266/NewList/NewList/List.c" create mode 100644 "Homework \342\204\2266/NewList/NewList/List.h" create mode 100644 "Homework \342\204\2266/NewList/NewList/Main.c" create mode 100644 "Homework \342\204\2266/NewList/NewList/NewList.vcxproj" create mode 100644 "Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" create mode 100644 "Homework \342\204\2266/NewList/NewList/TestList.c" create mode 100644 "Homework \342\204\2266/NewList/NewList/TestList.h" diff --git "a/Homework \342\204\2266/NewList/NewList.sln" "b/Homework \342\204\2266/NewList/NewList.sln" new file mode 100644 index 0000000..e7d3c04 --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList.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}") = "NewList", "NewList\NewList.vcxproj", "{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}" +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 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.ActiveCfg = Debug|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.Build.0 = Debug|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.ActiveCfg = Debug|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.Build.0 = Debug|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.ActiveCfg = Release|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.Build.0 = Release|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.ActiveCfg = Release|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3AA29672-FC57-4049-A441-2EC868F66725} + EndGlobalSection +EndGlobal diff --git "a/Homework \342\204\2266/NewList/NewList/List.c" "b/Homework \342\204\2266/NewList/NewList/List.c" new file mode 100644 index 0000000..bbaf88b --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList/List.c" @@ -0,0 +1,248 @@ +#include "List.h" +#include +#include +#include +#include + +// Structure containing pointers to the beginning and end of the list +typedef struct List +{ + int size; + struct ListElement* head; + struct ListElement* tail; +} List; + +// Structure containing a pointer to the next list item and a value variable for the list items +typedef struct ListElement +{ + char* firstValue; + char* secondValue; + struct ListElement* next; +} ListElement; + +// Structure containing a pointer to a ListElement +typedef struct Position +{ + ListElement* position; +} Position; + +List* createList() +{ + return calloc(1, sizeof(List)); +} + +void deleteList(List* list) +{ + ListElement* position = list->head; + while (position != NULL) + { + list->head = list->head->next; + free(position->secondValue); + free(position->firstValue); + free(position); + position = list->head; + } + free(list); +} + +void deletePosition(Position* position) +{ + free(position); +} + +void removeFirstElement(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + if (list->head == NULL) + { + *error = EMPTY_LIST; + return; + } + if (list->head == list->tail) + { + list->size = 0; + free(list->head->secondValue); + free(list->head->firstValue); + free(list->head); + list->head = NULL; + list->tail = NULL; + return; + } + ListElement* element = list->head; + list->head = list->head->next; + list->size--; + free(element->secondValue); + free(element->firstValue); + free(element); + +} + +Position* first(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionFirst->position = list->head; + return positionFirst; +} + +Position* last(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionLast = malloc(sizeof(Position)); + if (positionLast == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionLast->position = list->tail; + return positionLast; +} + +Position* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +bool isLastElement(Position* position) +{ + return position->position->next == NULL; +} + +int numberOfElements(List* list) +{ + return list->size; +} + +char* getHeadFirstValue(List* list) +{ + if (list->head == NULL) + { + return NULL; + } + return list->head->firstValue; +} + +char* getHeadSecondValue(List* list) +{ + if (list->head == NULL) + { + return NULL; + } + return list->head->secondValue; +} + +char* getFirstValue(Position* position) +{ + if (position->position == NULL) + { + return NULL; + } + return position->position->firstValue; +} + +char* getSecondValue(Position* position) +{ + if (position->position == NULL) + { + return NULL; + } + return position->position->secondValue; +} + +void add(List* list, char* firstValue, char* secondValue, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + char* firstValueCopy = calloc(100, sizeof(char)); + if (firstValueCopy == NULL) + { + *error = INSUFFICIENT_MEMORY; + return; + } + strcpy(firstValueCopy, firstValue); + char* secondValueCopy = calloc(100, sizeof(char)); + if (secondValueCopy == NULL) + { + free(firstValueCopy); + *error = 3; + return; + } + strcpy(secondValueCopy, secondValue); + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + *error = INSUFFICIENT_MEMORY; + return; + } + newElement->firstValue = firstValueCopy; + newElement->secondValue = secondValueCopy; + if (list->head == NULL) + { + list->size = 1; + list->head = newElement; + list->tail = newElement; + return; + } + list->size++; + list->tail->next = newElement; + list->tail = list->tail->next; +} + +bool isEmpty(List* list) +{ + return list->head == NULL; +} + +bool isOneElement(List* list) +{ + return list->head->next == NULL; +} + +void print(List* list) +{ + int error = 0; + ListElement* element = list->head; + while (element != NULL) + { + printf("%s ", element->firstValue); + printf("%s\n", element->secondValue); + element = element->next; + } +} + +bool compareList(List* firstList, List* secondList) +{ + ListElement* firstElement = firstList->head; + ListElement* secondElement = secondList->head; + if (numberOfElements(firstList) != numberOfElements(secondList)) + { + return false; + } + while (firstElement != NULL) + { + if (strcmp(firstElement->firstValue, secondElement->firstValue) != 0 || strcmp(firstElement->secondValue, secondElement->secondValue) != 0) + { + return false; + } + firstElement = firstElement->next; + secondElement = secondElement->next; + } + return true; +} \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/List.h" "b/Homework \342\204\2266/NewList/NewList/List.h" new file mode 100644 index 0000000..759c8c8 --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList/List.h" @@ -0,0 +1,70 @@ +#pragma once +#include + +// Structure that represents list +typedef struct List List; + +// This is a structure describing the position of an item in the list. +typedef struct Position Position; + +// Enum type for working with errors +typedef enum Error +{ + NOT_ERROR, + EMPTY_LIST, + INSUFFICIENT_MEMORY +}Error; + +// Function for creating a list +List* createList(); + +// Function for deleting a list +void deleteList(List* list); + +// Function for freeing up memory +void deletePosition(Position* position); + +// Function for adding an item to a list +void add(List* list, char* firstValue, char* secondValue , Error* error); + +// function to find a position to the first element +Position* first(List* list, Error* error); + +// Function for finding a position to the next element +Position* next(Position* position); + +// Function for checking an item for being at the end of the list +bool isLastElement(Position* position); + +// Function for printing a list +void print(List* list); + +// Function for deleting the first element +void removeFirstElement(List* list, Error* error); + +// Function for finding the number of items in the list +int numberOfElements(List* list); + +// Function for checking the emptiness of the list +bool isEmpty(List* list); + +// Function for checking the list for the content of a single element +bool isOneElement(List* list); + +// Function that returns a value for the first element +char* getHeadFirstValue(List* list); + +// Function that returns a value for the first element +char* getHeadSecondValue(List* list); + +// Function for finding a position to the last element +Position* last(List* list, Error* error); + +// Returns the value of any element +char* getFirstValue(Position* position); + +// Returns the value of any element +char* getSecondValue(Position* position); + +// Function for comparing lists +bool compareList(List* firstList, List* secondList); \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/Main.c" "b/Homework \342\204\2266/NewList/NewList/Main.c" new file mode 100644 index 0000000..4461872 --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList/Main.c" @@ -0,0 +1,11 @@ +#include "TestList.h" +#include + +int main() +{ + if (!allTest()) + { + printf("Test failed"); + return -1; + } +} \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/NewList.vcxproj" "b/Homework \342\204\2266/NewList/NewList/NewList.vcxproj" new file mode 100644 index 0000000..286a851 --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList/NewList.vcxproj" @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {8b99b0f3-fcb6-4875-8c2f-e0a7069b6043} + NewList + 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;_CRT_SECURE_NO_WARNINGS;_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/Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" "b/Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" new file mode 100644 index 0000000..230bb50 --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" @@ -0,0 +1,36 @@ + + + + + {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/Homework \342\204\2266/NewList/NewList/TestList.c" "b/Homework \342\204\2266/NewList/NewList/TestList.c" new file mode 100644 index 0000000..23b5911 --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList/TestList.c" @@ -0,0 +1,96 @@ +#include "TestList.h" +#include "List.h" +#include + +// Function to check the function that adds an item to the list +bool testAdd() +{ + Error error = NOT_ERROR; + List* newList = createList(); + add(newList, "test", " 1", &error); + add(newList, "list", "add", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* firstStringFirstValue = getHeadFirstValue(newList); + const char* firstStringSecondValue = getHeadSecondValue(newList); + Position* position = first(newList, &error); + if (error == EMPTY_LIST) + { + deleteList(newList); + return false; + } + next(position); + const char* secondStringFirstValue = getFirstValue(position); + const char* secondStringSecondValue = getSecondValue(position); + bool result = strcmp(firstStringFirstValue, "test") == 0 && strcmp(firstStringSecondValue, " 1") == 0 + && strcmp(secondStringFirstValue, "list") == 0 && strcmp(secondStringSecondValue, "add") == 0; + deletePosition(position); + deleteList(newList); + return result; +} + +// Function to check the function that deletes the first item in the list +bool testRemoveHead() +{ + Error error = NOT_ERROR; + List* newList = createList(); + add(newList, "Hello", "World", &error); + add(newList, "Merge", "Sort", &error); + removeFirstElement(newList, &error); + if (error == EMPTY_LIST || error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* headOfListFirstValue = getHeadFirstValue(newList); + const char* headOfListSecondValue = getHeadSecondValue(newList); + bool result = strcmp(headOfListFirstValue, "Merge") == 0 && strcmp(headOfListSecondValue, "Sort") == 0; + deleteList(newList); + return result; +} + +// Function for checking the function counting the number of elements +bool testNumberOfElements() +{ + List* newList = createList(); + Error error = NOT_ERROR; + add(newList, "Hello", "World", &error); + add(newList, "Merge", "Sort", &error); + add(newList, "test", " 1", &error); + add(newList, "list", "add", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const int number = numberOfElements(newList); + deleteList(newList); + return number == 4; +} + +// Function that checks the function that returns the value of the first element +bool testGetHeadFirstAndSecondValue() +{ + List* newList = createList(); + Error error = NOT_ERROR; + add(newList, "Hello", "World", &error); + add(newList, "Merge", "Sort", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* firstHeadValue = getHeadFirstValue(newList); + const char* secondHeadValue = getHeadSecondValue(newList); + bool result = strcmp(firstHeadValue, "Hello") == 0 && strcmp(secondHeadValue, "World") == 0; + deleteList(newList); + return result; +} + +bool allTest() +{ + return testAdd() && testGetHeadFirstAndSecondValue() && testNumberOfElements() && testRemoveHead(); +} \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/TestList.h" "b/Homework \342\204\2266/NewList/NewList/TestList.h" new file mode 100644 index 0000000..13ded01 --- /dev/null +++ "b/Homework \342\204\2266/NewList/NewList/TestList.h" @@ -0,0 +1,5 @@ +#pragma once +#include + +// A function that combines the results of all tests +bool allTest(); From 0efbf49911074e2e619eb92cca80a2c2780ff58d Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 26 Nov 2021 12:10:55 +0300 Subject: [PATCH 13/24] improved error handling --- SinglyLinkedList/NewList/NewList.sln | 31 +++ SinglyLinkedList/NewList/NewList/List.c | 263 ++++++++++++++++++ SinglyLinkedList/NewList/NewList/List.h | 73 +++++ SinglyLinkedList/NewList/NewList/Main.c | 11 + .../NewList/NewList/NewList.vcxproj | 153 ++++++++++ .../NewList/NewList/NewList.vcxproj.filters | 36 +++ SinglyLinkedList/NewList/NewList/TestList.c | 96 +++++++ SinglyLinkedList/NewList/NewList/TestList.h | 5 + 8 files changed, 668 insertions(+) create mode 100644 SinglyLinkedList/NewList/NewList.sln create mode 100644 SinglyLinkedList/NewList/NewList/List.c create mode 100644 SinglyLinkedList/NewList/NewList/List.h create mode 100644 SinglyLinkedList/NewList/NewList/Main.c create mode 100644 SinglyLinkedList/NewList/NewList/NewList.vcxproj create mode 100644 SinglyLinkedList/NewList/NewList/NewList.vcxproj.filters create mode 100644 SinglyLinkedList/NewList/NewList/TestList.c create mode 100644 SinglyLinkedList/NewList/NewList/TestList.h diff --git a/SinglyLinkedList/NewList/NewList.sln b/SinglyLinkedList/NewList/NewList.sln new file mode 100644 index 0000000..e7d3c04 --- /dev/null +++ b/SinglyLinkedList/NewList/NewList.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}") = "NewList", "NewList\NewList.vcxproj", "{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}" +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 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.ActiveCfg = Debug|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.Build.0 = Debug|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.ActiveCfg = Debug|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.Build.0 = Debug|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.ActiveCfg = Release|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.Build.0 = Release|x64 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.ActiveCfg = Release|Win32 + {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3AA29672-FC57-4049-A441-2EC868F66725} + EndGlobalSection +EndGlobal diff --git a/SinglyLinkedList/NewList/NewList/List.c b/SinglyLinkedList/NewList/NewList/List.c new file mode 100644 index 0000000..5972ee5 --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/List.c @@ -0,0 +1,263 @@ +#include "List.h" +#include +#include +#include +#include + +// Structure containing pointers to the beginning and end of the list +typedef struct List +{ + int size; + struct ListElement* head; + struct ListElement* tail; +} List; + +// Structure containing a pointer to the next list item and a value variable for the list items +typedef struct ListElement +{ + char* firstValue; + char* secondValue; + struct ListElement* next; +} ListElement; + +// Structure containing a pointer to a ListElement +typedef struct Position +{ + ListElement* position; +} Position; + +List* createList() +{ + return calloc(1, sizeof(List)); +} + +void deleteList(List* list) +{ + ListElement* position = list->head; + while (position != NULL) + { + list->head = list->head->next; + free(position->secondValue); + free(position->firstValue); + free(position); + position = list->head; + } + free(list); +} + +void deletePosition(Position* position) +{ + free(position); +} + +void removeFirstElement(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + if (list->head == NULL) + { + *error = EMPTY_LIST; + return; + } + if (list->head == list->tail) + { + list->size = 0; + free(list->head->secondValue); + free(list->head->firstValue); + free(list->head); + list->head = NULL; + list->tail = NULL; + return; + } + ListElement* element = list->head; + list->head = list->head->next; + list->size--; + free(element->secondValue); + free(element->firstValue); + free(element); + +} + +Position* first(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionFirst->position = list->head; + return positionFirst; +} + +Position* last(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionLast = malloc(sizeof(Position)); + if (positionLast == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionLast->position = list->tail; + return positionLast; +} + +Position* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +bool isLastElement(Position* position) +{ + return position->position->next == NULL; +} + +int numberOfElements(List* list) +{ + return list->size; +} + +char* getHeadFirstValue(List* list) +{ + if (list->head == NULL) + { + return NULL; + } + return list->head->firstValue; +} + +char* getHeadSecondValue(List* list) +{ + if (list->head == NULL) + { + return NULL; + } + return list->head->secondValue; +} + +char* getFirstValue(Position* position) +{ + if (position->position == NULL) + { + return NULL; + } + return position->position->firstValue; +} + +char* getSecondValue(Position* position) +{ + if (position->position == NULL) + { + return NULL; + } + return position->position->secondValue; +} + +void add(List* list, const char* firstValue, const char* secondValue, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + char* firstValueCopy = calloc(strlen(firstValue) + 1, sizeof(char)); + if (firstValueCopy == NULL) + { + *error = INSUFFICIENT_MEMORY; + return; + } + strcpy(firstValueCopy, firstValue); + char* secondValueCopy = calloc(strlen(secondValue) + 1, sizeof(char)); + if (secondValueCopy == NULL) + { + free(firstValueCopy); + *error = 3; + return; + } + strcpy(secondValueCopy, secondValue); + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + free(firstValueCopy); + free(secondValueCopy); + *error = INSUFFICIENT_MEMORY; + return; + } + newElement->firstValue = firstValueCopy; + newElement->secondValue = secondValueCopy; + if (list->head == NULL) + { + list->size = 1; + list->head = newElement; + list->tail = newElement; + return; + } + list->size++; + list->tail->next = newElement; + list->tail = list->tail->next; +} + +bool isEmpty(List* list) +{ + return list->head == NULL; +} + +bool isOneElement(List* list) +{ + return list->head->next == NULL; +} + +void print(List* list) +{ + int error = 0; + ListElement* element = list->head; + while (element != NULL) + { + printf("%s ", element->firstValue); + printf("%s\n", element->secondValue); + element = element->next; + } +} + +bool compareList(List* firstList, List* secondList) +{ + ListElement* firstElement = firstList->head; + ListElement* secondElement = secondList->head; + if (numberOfElements(firstList) != numberOfElements(secondList)) + { + return false; + } + while (firstElement != NULL) + { + if (strcmp(firstElement->firstValue, secondElement->firstValue) != 0 || strcmp(firstElement->secondValue, secondElement->secondValue) != 0) + { + return false; + } + firstElement = firstElement->next; + secondElement = secondElement->next; + } + return true; +} + +const char* decodingError(Error error) +{ + if (error == EMPTY_LIST) + { + return "Error in the program"; + } + if (error == INSUFFICIENT_MEMORY) + { + return "Memory not allocated"; + } + return NULL; +} \ No newline at end of file diff --git a/SinglyLinkedList/NewList/NewList/List.h b/SinglyLinkedList/NewList/NewList/List.h new file mode 100644 index 0000000..808f752 --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/List.h @@ -0,0 +1,73 @@ +#pragma once +#include + +// Structure that represents list +typedef struct List List; + +// This is a structure describing the position of an item in the list. +typedef struct Position Position; + +// Enum type for working with errors +typedef enum Error +{ + NOT_ERROR, + EMPTY_LIST, + INSUFFICIENT_MEMORY +}Error; + +// Function for creating a list +List* createList(); + +// Function for deleting a list +void deleteList(List* list); + +// Function for freeing up memory +void deletePosition(Position* position); + +// Function for adding an item to a list +void add(List* list, const char* firstValue, const char* secondValue, Error* error); + +// function to find a position to the first element +Position* first(List* list, Error* error); + +// Function for finding a position to the next element +Position* next(Position* position); + +// Function for checking an item for being at the end of the list +bool isLastElement(Position* position); + +// Function for printing a list +void print(List* list); + +// Function for deleting the first element +void removeFirstElement(List* list, Error* error); + +// Function for finding the number of items in the list +int numberOfElements(List* list); + +// Function for checking the emptiness of the list +bool isEmpty(List* list); + +// Function for checking the list for the content of a single element +bool isOneElement(List* list); + +// Function that returns a value for the first element +char* getHeadFirstValue(List* list); + +// Function that returns a value for the first element +char* getHeadSecondValue(List* list); + +// Function for finding a position to the last element +Position* last(List* list, Error* error); + +// Returns the value of any element +char* getFirstValue(Position* position); + +// Returns the value of any element +char* getSecondValue(Position* position); + +// Function for comparing lists +bool compareList(List* firstList, List* secondList); + +// Function for error decoding +const char* decodingError(Error error); \ No newline at end of file diff --git a/SinglyLinkedList/NewList/NewList/Main.c b/SinglyLinkedList/NewList/NewList/Main.c new file mode 100644 index 0000000..4461872 --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/Main.c @@ -0,0 +1,11 @@ +#include "TestList.h" +#include + +int main() +{ + if (!allTest()) + { + printf("Test failed"); + return -1; + } +} \ No newline at end of file diff --git a/SinglyLinkedList/NewList/NewList/NewList.vcxproj b/SinglyLinkedList/NewList/NewList/NewList.vcxproj new file mode 100644 index 0000000..4760bae --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/NewList.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {8b99b0f3-fcb6-4875-8c2f-e0a7069b6043} + NewList + 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;_CRT_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(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/SinglyLinkedList/NewList/NewList/NewList.vcxproj.filters b/SinglyLinkedList/NewList/NewList/NewList.vcxproj.filters new file mode 100644 index 0000000..230bb50 --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/NewList.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {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/SinglyLinkedList/NewList/NewList/TestList.c b/SinglyLinkedList/NewList/NewList/TestList.c new file mode 100644 index 0000000..aba0f7a --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/TestList.c @@ -0,0 +1,96 @@ +#include "TestList.h" +#include "List.h" +#include + +// Function to check the function that adds an item to the list +bool testAdd() +{ + Error error = NOT_ERROR; + List* newList = createList(); + add(newList, "test", "1", &error); + add(newList, "list", "add", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* firstStringFirstValue = getHeadFirstValue(newList); + const char* firstStringSecondValue = getHeadSecondValue(newList); + Position* position = first(newList, &error); + if (error == EMPTY_LIST) + { + deleteList(newList); + return false; + } + next(position); + const char* secondStringFirstValue = getFirstValue(position); + const char* secondStringSecondValue = getSecondValue(position); + bool result = strcmp(firstStringFirstValue, "test") == 0 && strcmp(firstStringSecondValue, "1") == 0 + && strcmp(secondStringFirstValue, "list") == 0 && strcmp(secondStringSecondValue, "add") == 0; + deletePosition(position); + deleteList(newList); + return result; +} + +// Function to check the function that deletes the first item in the list +bool testRemoveHead() +{ + Error error = NOT_ERROR; + List* newList = createList(); + add(newList, "Hello", "World", &error); + add(newList, "Merge", "Sort", &error); + removeFirstElement(newList, &error); + if (error == EMPTY_LIST || error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* headOfListFirstValue = getHeadFirstValue(newList); + const char* headOfListSecondValue = getHeadSecondValue(newList); + bool result = strcmp(headOfListFirstValue, "Merge") == 0 && strcmp(headOfListSecondValue, "Sort") == 0; + deleteList(newList); + return result; +} + +// Function for checking the function counting the number of elements +bool testNumberOfElements() +{ + List* newList = createList(); + Error error = NOT_ERROR; + add(newList, "Hello", "World", &error); + add(newList, "Merge", "Sort", &error); + add(newList, "test", "1", &error); + add(newList, "list", "add", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const int number = numberOfElements(newList); + deleteList(newList); + return number == 4; +} + +// Function that checks the function that returns the value of the first element +bool testGetHeadFirstAndSecondValue() +{ + List* newList = createList(); + Error error = NOT_ERROR; + add(newList, "Hello", "World", &error); + add(newList, "Merge", "Sort", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* firstHeadValue = getHeadFirstValue(newList); + const char* secondHeadValue = getHeadSecondValue(newList); + bool result = strcmp(firstHeadValue, "Hello") == 0 && strcmp(secondHeadValue, "World") == 0; + deleteList(newList); + return result; +} + +bool allTest() +{ + return testAdd() && testGetHeadFirstAndSecondValue() && testNumberOfElements() && testRemoveHead(); +} \ No newline at end of file diff --git a/SinglyLinkedList/NewList/NewList/TestList.h b/SinglyLinkedList/NewList/NewList/TestList.h new file mode 100644 index 0000000..13ded01 --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/TestList.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// A function that combines the results of all tests +bool allTest(); From efe9debc5c29089f19f19958853d2f241ed7d800 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 26 Nov 2021 12:25:33 +0300 Subject: [PATCH 14/24] moving files --- "Homework \342\204\2266/NewList/NewList.sln" | 31 --- .../NewList/NewList/List.c" | 248 ------------------ .../NewList/NewList/List.h" | 70 ----- .../NewList/NewList/Main.c" | 11 - .../NewList/NewList/NewList.vcxproj" | 153 ----------- .../NewList/NewList/NewList.vcxproj.filters" | 36 --- .../NewList/NewList/TestList.c" | 96 ------- .../NewList/NewList/TestList.h" | 5 - NewList/NewList.sln | 31 --- NewList/NewList/List.c | 234 ----------------- NewList/NewList/List.h | 62 ----- NewList/NewList/Main.c | 11 - NewList/NewList/NewList.vcxproj | 153 ----------- NewList/NewList/NewList.vcxproj.filters | 36 --- NewList/NewList/TestList.c | 116 -------- NewList/NewList/TestList.h | 5 - 16 files changed, 1298 deletions(-) delete mode 100644 "Homework \342\204\2266/NewList/NewList.sln" delete mode 100644 "Homework \342\204\2266/NewList/NewList/List.c" delete mode 100644 "Homework \342\204\2266/NewList/NewList/List.h" delete mode 100644 "Homework \342\204\2266/NewList/NewList/Main.c" delete mode 100644 "Homework \342\204\2266/NewList/NewList/NewList.vcxproj" delete mode 100644 "Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" delete mode 100644 "Homework \342\204\2266/NewList/NewList/TestList.c" delete mode 100644 "Homework \342\204\2266/NewList/NewList/TestList.h" delete mode 100644 NewList/NewList.sln delete mode 100644 NewList/NewList/List.c delete mode 100644 NewList/NewList/List.h delete mode 100644 NewList/NewList/Main.c delete mode 100644 NewList/NewList/NewList.vcxproj delete mode 100644 NewList/NewList/NewList.vcxproj.filters delete mode 100644 NewList/NewList/TestList.c delete mode 100644 NewList/NewList/TestList.h diff --git "a/Homework \342\204\2266/NewList/NewList.sln" "b/Homework \342\204\2266/NewList/NewList.sln" deleted file mode 100644 index e7d3c04..0000000 --- "a/Homework \342\204\2266/NewList/NewList.sln" +++ /dev/null @@ -1,31 +0,0 @@ - -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}") = "NewList", "NewList\NewList.vcxproj", "{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}" -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 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.ActiveCfg = Debug|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.Build.0 = Debug|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.ActiveCfg = Debug|Win32 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.Build.0 = Debug|Win32 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.ActiveCfg = Release|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.Build.0 = Release|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.ActiveCfg = Release|Win32 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {3AA29672-FC57-4049-A441-2EC868F66725} - EndGlobalSection -EndGlobal diff --git "a/Homework \342\204\2266/NewList/NewList/List.c" "b/Homework \342\204\2266/NewList/NewList/List.c" deleted file mode 100644 index bbaf88b..0000000 --- "a/Homework \342\204\2266/NewList/NewList/List.c" +++ /dev/null @@ -1,248 +0,0 @@ -#include "List.h" -#include -#include -#include -#include - -// Structure containing pointers to the beginning and end of the list -typedef struct List -{ - int size; - struct ListElement* head; - struct ListElement* tail; -} List; - -// Structure containing a pointer to the next list item and a value variable for the list items -typedef struct ListElement -{ - char* firstValue; - char* secondValue; - struct ListElement* next; -} ListElement; - -// Structure containing a pointer to a ListElement -typedef struct Position -{ - ListElement* position; -} Position; - -List* createList() -{ - return calloc(1, sizeof(List)); -} - -void deleteList(List* list) -{ - ListElement* position = list->head; - while (position != NULL) - { - list->head = list->head->next; - free(position->secondValue); - free(position->firstValue); - free(position); - position = list->head; - } - free(list); -} - -void deletePosition(Position* position) -{ - free(position); -} - -void removeFirstElement(List* list, Error* error) -{ - if (*error != NOT_ERROR) - { - return; - } - if (list->head == NULL) - { - *error = EMPTY_LIST; - return; - } - if (list->head == list->tail) - { - list->size = 0; - free(list->head->secondValue); - free(list->head->firstValue); - free(list->head); - list->head = NULL; - list->tail = NULL; - return; - } - ListElement* element = list->head; - list->head = list->head->next; - list->size--; - free(element->secondValue); - free(element->firstValue); - free(element); - -} - -Position* first(List* list, Error* error) -{ - if (*error != NOT_ERROR) - { - return NULL; - } - Position* positionFirst = malloc(sizeof(Position)); - if (positionFirst == NULL) - { - *error = EMPTY_LIST; - return NULL; - } - positionFirst->position = list->head; - return positionFirst; -} - -Position* last(List* list, Error* error) -{ - if (*error != NOT_ERROR) - { - return NULL; - } - Position* positionLast = malloc(sizeof(Position)); - if (positionLast == NULL) - { - *error = EMPTY_LIST; - return NULL; - } - positionLast->position = list->tail; - return positionLast; -} - -Position* next(Position* position) -{ - position->position = position->position->next; - return position; -} - -bool isLastElement(Position* position) -{ - return position->position->next == NULL; -} - -int numberOfElements(List* list) -{ - return list->size; -} - -char* getHeadFirstValue(List* list) -{ - if (list->head == NULL) - { - return NULL; - } - return list->head->firstValue; -} - -char* getHeadSecondValue(List* list) -{ - if (list->head == NULL) - { - return NULL; - } - return list->head->secondValue; -} - -char* getFirstValue(Position* position) -{ - if (position->position == NULL) - { - return NULL; - } - return position->position->firstValue; -} - -char* getSecondValue(Position* position) -{ - if (position->position == NULL) - { - return NULL; - } - return position->position->secondValue; -} - -void add(List* list, char* firstValue, char* secondValue, Error* error) -{ - if (*error != NOT_ERROR) - { - return; - } - char* firstValueCopy = calloc(100, sizeof(char)); - if (firstValueCopy == NULL) - { - *error = INSUFFICIENT_MEMORY; - return; - } - strcpy(firstValueCopy, firstValue); - char* secondValueCopy = calloc(100, sizeof(char)); - if (secondValueCopy == NULL) - { - free(firstValueCopy); - *error = 3; - return; - } - strcpy(secondValueCopy, secondValue); - ListElement* newElement = calloc(1, sizeof(ListElement)); - if (newElement == NULL) - { - *error = INSUFFICIENT_MEMORY; - return; - } - newElement->firstValue = firstValueCopy; - newElement->secondValue = secondValueCopy; - if (list->head == NULL) - { - list->size = 1; - list->head = newElement; - list->tail = newElement; - return; - } - list->size++; - list->tail->next = newElement; - list->tail = list->tail->next; -} - -bool isEmpty(List* list) -{ - return list->head == NULL; -} - -bool isOneElement(List* list) -{ - return list->head->next == NULL; -} - -void print(List* list) -{ - int error = 0; - ListElement* element = list->head; - while (element != NULL) - { - printf("%s ", element->firstValue); - printf("%s\n", element->secondValue); - element = element->next; - } -} - -bool compareList(List* firstList, List* secondList) -{ - ListElement* firstElement = firstList->head; - ListElement* secondElement = secondList->head; - if (numberOfElements(firstList) != numberOfElements(secondList)) - { - return false; - } - while (firstElement != NULL) - { - if (strcmp(firstElement->firstValue, secondElement->firstValue) != 0 || strcmp(firstElement->secondValue, secondElement->secondValue) != 0) - { - return false; - } - firstElement = firstElement->next; - secondElement = secondElement->next; - } - return true; -} \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/List.h" "b/Homework \342\204\2266/NewList/NewList/List.h" deleted file mode 100644 index 759c8c8..0000000 --- "a/Homework \342\204\2266/NewList/NewList/List.h" +++ /dev/null @@ -1,70 +0,0 @@ -#pragma once -#include - -// Structure that represents list -typedef struct List List; - -// This is a structure describing the position of an item in the list. -typedef struct Position Position; - -// Enum type for working with errors -typedef enum Error -{ - NOT_ERROR, - EMPTY_LIST, - INSUFFICIENT_MEMORY -}Error; - -// Function for creating a list -List* createList(); - -// Function for deleting a list -void deleteList(List* list); - -// Function for freeing up memory -void deletePosition(Position* position); - -// Function for adding an item to a list -void add(List* list, char* firstValue, char* secondValue , Error* error); - -// function to find a position to the first element -Position* first(List* list, Error* error); - -// Function for finding a position to the next element -Position* next(Position* position); - -// Function for checking an item for being at the end of the list -bool isLastElement(Position* position); - -// Function for printing a list -void print(List* list); - -// Function for deleting the first element -void removeFirstElement(List* list, Error* error); - -// Function for finding the number of items in the list -int numberOfElements(List* list); - -// Function for checking the emptiness of the list -bool isEmpty(List* list); - -// Function for checking the list for the content of a single element -bool isOneElement(List* list); - -// Function that returns a value for the first element -char* getHeadFirstValue(List* list); - -// Function that returns a value for the first element -char* getHeadSecondValue(List* list); - -// Function for finding a position to the last element -Position* last(List* list, Error* error); - -// Returns the value of any element -char* getFirstValue(Position* position); - -// Returns the value of any element -char* getSecondValue(Position* position); - -// Function for comparing lists -bool compareList(List* firstList, List* secondList); \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/Main.c" "b/Homework \342\204\2266/NewList/NewList/Main.c" deleted file mode 100644 index 4461872..0000000 --- "a/Homework \342\204\2266/NewList/NewList/Main.c" +++ /dev/null @@ -1,11 +0,0 @@ -#include "TestList.h" -#include - -int main() -{ - if (!allTest()) - { - printf("Test failed"); - return -1; - } -} \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/NewList.vcxproj" "b/Homework \342\204\2266/NewList/NewList/NewList.vcxproj" deleted file mode 100644 index 286a851..0000000 --- "a/Homework \342\204\2266/NewList/NewList/NewList.vcxproj" +++ /dev/null @@ -1,153 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - Win32Proj - {8b99b0f3-fcb6-4875-8c2f-e0a7069b6043} - NewList - 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;_CRT_SECURE_NO_WARNINGS;_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/Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" "b/Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" deleted file mode 100644 index 230bb50..0000000 --- "a/Homework \342\204\2266/NewList/NewList/NewList.vcxproj.filters" +++ /dev/null @@ -1,36 +0,0 @@ - - - - - {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/Homework \342\204\2266/NewList/NewList/TestList.c" "b/Homework \342\204\2266/NewList/NewList/TestList.c" deleted file mode 100644 index 23b5911..0000000 --- "a/Homework \342\204\2266/NewList/NewList/TestList.c" +++ /dev/null @@ -1,96 +0,0 @@ -#include "TestList.h" -#include "List.h" -#include - -// Function to check the function that adds an item to the list -bool testAdd() -{ - Error error = NOT_ERROR; - List* newList = createList(); - add(newList, "test", " 1", &error); - add(newList, "list", "add", &error); - if (error == INSUFFICIENT_MEMORY) - { - deleteList(newList); - return false; - } - const char* firstStringFirstValue = getHeadFirstValue(newList); - const char* firstStringSecondValue = getHeadSecondValue(newList); - Position* position = first(newList, &error); - if (error == EMPTY_LIST) - { - deleteList(newList); - return false; - } - next(position); - const char* secondStringFirstValue = getFirstValue(position); - const char* secondStringSecondValue = getSecondValue(position); - bool result = strcmp(firstStringFirstValue, "test") == 0 && strcmp(firstStringSecondValue, " 1") == 0 - && strcmp(secondStringFirstValue, "list") == 0 && strcmp(secondStringSecondValue, "add") == 0; - deletePosition(position); - deleteList(newList); - return result; -} - -// Function to check the function that deletes the first item in the list -bool testRemoveHead() -{ - Error error = NOT_ERROR; - List* newList = createList(); - add(newList, "Hello", "World", &error); - add(newList, "Merge", "Sort", &error); - removeFirstElement(newList, &error); - if (error == EMPTY_LIST || error == INSUFFICIENT_MEMORY) - { - deleteList(newList); - return false; - } - const char* headOfListFirstValue = getHeadFirstValue(newList); - const char* headOfListSecondValue = getHeadSecondValue(newList); - bool result = strcmp(headOfListFirstValue, "Merge") == 0 && strcmp(headOfListSecondValue, "Sort") == 0; - deleteList(newList); - return result; -} - -// Function for checking the function counting the number of elements -bool testNumberOfElements() -{ - List* newList = createList(); - Error error = NOT_ERROR; - add(newList, "Hello", "World", &error); - add(newList, "Merge", "Sort", &error); - add(newList, "test", " 1", &error); - add(newList, "list", "add", &error); - if (error == INSUFFICIENT_MEMORY) - { - deleteList(newList); - return false; - } - const int number = numberOfElements(newList); - deleteList(newList); - return number == 4; -} - -// Function that checks the function that returns the value of the first element -bool testGetHeadFirstAndSecondValue() -{ - List* newList = createList(); - Error error = NOT_ERROR; - add(newList, "Hello", "World", &error); - add(newList, "Merge", "Sort", &error); - if (error == INSUFFICIENT_MEMORY) - { - deleteList(newList); - return false; - } - const char* firstHeadValue = getHeadFirstValue(newList); - const char* secondHeadValue = getHeadSecondValue(newList); - bool result = strcmp(firstHeadValue, "Hello") == 0 && strcmp(secondHeadValue, "World") == 0; - deleteList(newList); - return result; -} - -bool allTest() -{ - return testAdd() && testGetHeadFirstAndSecondValue() && testNumberOfElements() && testRemoveHead(); -} \ No newline at end of file diff --git "a/Homework \342\204\2266/NewList/NewList/TestList.h" "b/Homework \342\204\2266/NewList/NewList/TestList.h" deleted file mode 100644 index 13ded01..0000000 --- "a/Homework \342\204\2266/NewList/NewList/TestList.h" +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include - -// A function that combines the results of all tests -bool allTest(); diff --git a/NewList/NewList.sln b/NewList/NewList.sln deleted file mode 100644 index e7d3c04..0000000 --- a/NewList/NewList.sln +++ /dev/null @@ -1,31 +0,0 @@ - -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}") = "NewList", "NewList\NewList.vcxproj", "{8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}" -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 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.ActiveCfg = Debug|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x64.Build.0 = Debug|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.ActiveCfg = Debug|Win32 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Debug|x86.Build.0 = Debug|Win32 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.ActiveCfg = Release|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x64.Build.0 = Release|x64 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.ActiveCfg = Release|Win32 - {8B99B0F3-FCB6-4875-8C2F-E0A7069B6043}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {3AA29672-FC57-4049-A441-2EC868F66725} - EndGlobalSection -EndGlobal diff --git a/NewList/NewList/List.c b/NewList/NewList/List.c deleted file mode 100644 index c3dba4d..0000000 --- a/NewList/NewList/List.c +++ /dev/null @@ -1,234 +0,0 @@ -#include "List.h" -#include -#include -#include -#include - -// Structure containing pointers to the beginning and end of the list -typedef struct List -{ - int size; - struct ListElement* head; - struct ListElement* tail; -} List; - -// Structure containing a pointer to the next list item and a value variable for the list items -typedef struct ListElement -{ - char* firstValue; - char* secondValue; - struct ListElement* next; -} ListElement; - -// Structure containing a pointer to a ListElement -typedef struct Position -{ - ListElement* position; -} Position; - -List* createList() -{ - return calloc(1, sizeof(List)); -} - -void deleteList(List* list) -{ - ListElement* position = list->head; - while (position != NULL) - { - list->head = list->head->next; - free(position->secondValue); - free(position->firstValue); - free(position); - position = list->head; - } - free(list); -} - -void deletePosition(Position* position) -{ - free(position); -} - -void removeFirstElement(List* list, int* error) -{ - *error = 0; - if (list->head == NULL) - { - *error = 2; - return; - } - if (list->head == list->tail) - { - list->size = 0; - free(list->head->secondValue); - free(list->head->firstValue); - free(list->head); - list->head = NULL; - list->tail = NULL; - return; - } - ListElement* element = list->head; - list->head = list->head->next; - list->size--; - free(element->secondValue); - free(element->firstValue); - free(element); - -} - -Position* first(List* list, int* error) -{ - *error = 0; - Position* positionFirst = malloc(sizeof(Position)); - if (positionFirst == NULL) - { - *error = 3; - return NULL; - } - positionFirst->position = list->head; - return positionFirst; -} - -Position* last(List* list, int* error) -{ - Position* positionLast = malloc(sizeof(Position)); - if (positionLast == NULL) - { - *error = 3; - return NULL; - } - positionLast->position = list->tail; - return positionLast; -} - -Position* next(Position* position) -{ - position->position = position->position->next; - return position; -} - -bool isLastElement(Position* position) -{ - return position->position->next == NULL; -} - -int numberOfElements(List* list) -{ - return list->size; -} - -char* getHeadFirstValue(List* list) -{ - if (list->head == NULL) - { - return NULL; - } - return list->head->firstValue; -} - -char* getHeadSecondValue(List* list) -{ - if (list->head == NULL) - { - return NULL; - } - return list->head->secondValue; -} - -char* getFirstValue(Position* position) -{ - if (position->position == NULL) - { - return NULL; - } - return position->position->firstValue; -} - -char* getSecondValue(Position* position) -{ - if (position->position == NULL) - { - return NULL; - } - return position->position->secondValue; -} - -void add(List* list, char* firstValue, char* secondValue, int* error) -{ - char* firstValueCopy = calloc(100, sizeof(char)); - if (firstValueCopy == NULL) - { - *error = 3; - return; - } - strcpy(firstValueCopy, firstValue); - char* secondValueCopy = calloc(100, sizeof(char)); - if (secondValueCopy == NULL) - { - free(firstValueCopy); - *error = 3; - return; - } - strcpy(secondValueCopy, secondValue); - ListElement* newElement = calloc(1, sizeof(ListElement)); - if (newElement == NULL) - { - *error = 3; - return; - } - newElement->firstValue = firstValueCopy; - newElement->secondValue = secondValueCopy; - if (list->head == NULL) - { - list->size = 1; - list->head = newElement; - list->tail = newElement; - return; - } - list->size++; - list->tail->next = newElement; - list->tail = list->tail->next; -} - -bool isEmpty(List* list) -{ - return list->head == NULL; -} - -bool isOneElement(List* list) -{ - return list->head->next == NULL; -} - -void print(List* list) -{ - int error = 0; - ListElement* element = list->head; - while (element != NULL) - { - printf("%s ", element->firstValue); - printf("%s\n", element->secondValue); - element = element->next; - } -} - -bool compareList(List* firstList, List* secondList) -{ - ListElement* firstElement = firstList->head; - ListElement* secondElement = secondList->head; - if (numberOfElements(firstList) != numberOfElements(secondList)) - { - return false; - } - while (firstElement != NULL) - { - if (strcmp(firstElement->firstValue, secondElement->firstValue) != 0 || strcmp(firstElement->secondValue, secondElement->secondValue) != 0) - { - return false; - } - firstElement = firstElement->next; - secondElement = secondElement->next; - } - return true; -} \ No newline at end of file diff --git a/NewList/NewList/List.h b/NewList/NewList/List.h deleted file mode 100644 index 05c488b..0000000 --- a/NewList/NewList/List.h +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once -#include - -// Structure that represents list -typedef struct List List; - -// This is a structure describing the position of an item in the list. -typedef struct Position Position; - -// Function for creating a list -List* createList(); - -// Function for deleting a list -void deleteList(List* list); - -// Function for freeing up memory -void deletePosition(Position* position); - -// Function for adding an item to a list -void add(List* list, char* firstValue, char* secondValue , int* error); - -// function to find a position to the first element -Position* first(List* list, int* error); - -// Function for finding a position to the next element -Position* next(Position* position); - -// Function for checking an item for being at the end of the list -bool isLastElement(Position* position); - -// Function for printing a list -void print(List* list); - -// Function for deleting the first element -void removeFirstElement(List* list, int* error); - -// Function for finding the number of items in the list -int numberOfElements(List* list); - -// Function for checking the emptiness of the list -bool isEmpty(List* list); - -// Function for checking the list for the content of a single element -bool isOneElement(List* list); - -// Function that returns a value for the first element -char* getHeadFirstValue(List* list); - -// Function that returns a value for the first element -char* getHeadSecondValue(List* list); - -// Function for finding a position to the last element -Position* last(List* list, int* error); - -// Returns the value of any element -char* getFirstValue(Position* position); - -// Returns the value of any element -char* getSecondValue(Position* position); - -// Function for comparing lists -bool compareList(List* firstList, List* secondList); \ No newline at end of file diff --git a/NewList/NewList/Main.c b/NewList/NewList/Main.c deleted file mode 100644 index 4461872..0000000 --- a/NewList/NewList/Main.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "TestList.h" -#include - -int main() -{ - if (!allTest()) - { - printf("Test failed"); - return -1; - } -} \ No newline at end of file diff --git a/NewList/NewList/NewList.vcxproj b/NewList/NewList/NewList.vcxproj deleted file mode 100644 index 286a851..0000000 --- a/NewList/NewList/NewList.vcxproj +++ /dev/null @@ -1,153 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - Win32Proj - {8b99b0f3-fcb6-4875-8c2f-e0a7069b6043} - NewList - 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;_CRT_SECURE_NO_WARNINGS;_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/NewList/NewList/NewList.vcxproj.filters b/NewList/NewList/NewList.vcxproj.filters deleted file mode 100644 index 230bb50..0000000 --- a/NewList/NewList/NewList.vcxproj.filters +++ /dev/null @@ -1,36 +0,0 @@ - - - - - {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/NewList/NewList/TestList.c b/NewList/NewList/TestList.c deleted file mode 100644 index 615c82c..0000000 --- a/NewList/NewList/TestList.c +++ /dev/null @@ -1,116 +0,0 @@ -#include "TestList.h" -#include "List.h" -#include - -// Function to check the function that adds an item to the list -bool testAdd() -{ - int error = 0; - List* newList = createList(); - add(newList, "test", " 1", &error); - add(newList, "list", "add", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - const char* firstStringFirstValue = getHeadFirstValue(newList); - const char* firstStringSecondValue = getHeadSecondValue(newList); - Position* position = first(newList, &error); - next(position); - const char* secondStringFirstValue = getFirstValue(position); - const char* secondStringSecondValue = getSecondValue(position); - bool result = strcmp(firstStringFirstValue, "test") == 0 && strcmp(firstStringSecondValue, " 1") == 0 - && strcmp(secondStringFirstValue, "list") == 0 && strcmp(secondStringSecondValue, "add") == 0; - deletePosition(position); - deleteList(newList); - return result; -} - -// Function to check the function that deletes the first item in the list -bool testRemoveHead() -{ - int error = 0; - List* newList = createList(); - add(newList, "Hello", "World", &error); - add(newList, "Merge", "Sort", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - removeFirstElement(newList, &error); - if (error == 2) - { - deleteList(newList); - return false; - } - const char* headOfListFirstValue = getHeadFirstValue(newList); - const char* headOfListSecondValue = getHeadSecondValue(newList); - bool result = strcmp(headOfListFirstValue, "Merge") == 0 && strcmp(headOfListSecondValue, "Sort") == 0; - deleteList(newList); - return result; -} - -// Function for checking the function counting the number of elements -bool testNumberOfElements() -{ - List* newList = createList(); - int error = 0; - add(newList, "Hello", "World", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - add(newList, "Merge", "Sort", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - add(newList, "test", " 1", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - add(newList, "list", "add", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - const int number = numberOfElements(newList); - deleteList(newList); - return number == 4; -} - -// Function that checks the function that returns the value of the first element -bool testGetHeadFirstAndSecondValue() -{ - List* newList = createList(); - int error = 0; - add(newList, "Hello", "World", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - add(newList, "Merge", "Sort", &error); - if (error == 3) - { - deleteList(newList); - return false; - } - const char* firstHeadValue = getHeadFirstValue(newList); - const char* secondHeadValue = getHeadSecondValue(newList); - bool result = strcmp(firstHeadValue, "Hello") == 0 && strcmp(secondHeadValue, "World") == 0; - deleteList(newList); - return result; -} - -bool allTest() -{ - return testAdd() && testGetHeadFirstAndSecondValue() && testNumberOfElements() && testRemoveHead(); -} \ No newline at end of file diff --git a/NewList/NewList/TestList.h b/NewList/NewList/TestList.h deleted file mode 100644 index 13ded01..0000000 --- a/NewList/NewList/TestList.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include - -// A function that combines the results of all tests -bool allTest(); From f3b2155d4842ff7d30b3816d03026a3733530055 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 26 Nov 2021 13:09:43 +0300 Subject: [PATCH 15/24] put a space --- SinglyLinkedList/NewList/NewList/List.c | 1 - SinglyLinkedList/NewList/NewList/List.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/SinglyLinkedList/NewList/NewList/List.c b/SinglyLinkedList/NewList/NewList/List.c index 5972ee5..e690707 100644 --- a/SinglyLinkedList/NewList/NewList/List.c +++ b/SinglyLinkedList/NewList/NewList/List.c @@ -219,7 +219,6 @@ bool isOneElement(List* list) void print(List* list) { - int error = 0; ListElement* element = list->head; while (element != NULL) { diff --git a/SinglyLinkedList/NewList/NewList/List.h b/SinglyLinkedList/NewList/NewList/List.h index 808f752..18a8b17 100644 --- a/SinglyLinkedList/NewList/NewList/List.h +++ b/SinglyLinkedList/NewList/NewList/List.h @@ -13,7 +13,7 @@ typedef enum Error NOT_ERROR, EMPTY_LIST, INSUFFICIENT_MEMORY -}Error; +} Error; // Function for creating a list List* createList(); From e7ffca81f007f6ab352943690a3315953e34a4b5 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 27 Nov 2021 20:37:41 +0300 Subject: [PATCH 16/24] changed error handling for some list function --- .../SinglyLinkedListForHashTable.sln | 31 +++ .../SinglyLinkedListForHashTable/Main.c | 9 + .../SinglyLinkedListForHashTable.c | 256 ++++++++++++++++++ .../SinglyLinkedListForHashTable.h | 59 ++++ .../SinglyLinkedListForHashTable.vcxproj | 153 +++++++++++ ...nglyLinkedListForHashTable.vcxproj.filters | 36 +++ .../TestSinglyLinkedListForHashTable.c | 98 +++++++ .../TestSinglyLinkedListForHashTable.h | 5 + 8 files changed, 647 insertions(+) create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.sln create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/Main.c create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj.filters create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c create mode 100644 SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.h diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.sln b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.sln new file mode 100644 index 0000000..d81aa68 --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.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}") = "SinglyLinkedListForHashTable", "SinglyLinkedListForHashTable\SinglyLinkedListForHashTable.vcxproj", "{ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}" +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 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Debug|x64.ActiveCfg = Debug|x64 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Debug|x64.Build.0 = Debug|x64 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Debug|x86.ActiveCfg = Debug|Win32 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Debug|x86.Build.0 = Debug|Win32 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Release|x64.ActiveCfg = Release|x64 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Release|x64.Build.0 = Release|x64 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Release|x86.ActiveCfg = Release|Win32 + {ABF8D20F-CBDB-4353-9BCC-D9ECC7866F3E}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {91BE3025-E290-44D1-8BF0-D958E20D7DD7} + EndGlobalSection +EndGlobal diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/Main.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/Main.c new file mode 100644 index 0000000..3be54fc --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/Main.c @@ -0,0 +1,9 @@ +#include "TestSinglyLinkedListForHashTable.h" + +int main() +{ + if (!allTest()) + { + return -1; + } +} \ No newline at end of file diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c new file mode 100644 index 0000000..fd6d1cf --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c @@ -0,0 +1,256 @@ +#include "SinglyLinkedListForHashTable.h" +#include +#include +#include +#include + +// Structure containing pointers to the beginning and end of the list +typedef struct List +{ + int size; + struct ListElement* head; + struct ListElement* tail; +} List; + +// Structure containing a pointer to the next list item and a value variable for the list items +typedef struct ListElement +{ + int numberOfDuplicateValues; + char* value; + struct ListElement* next; +} ListElement; + +// Structure containing a pointer to a ListElement +typedef struct Position +{ + ListElement* position; +} Position; + +List* createList(Error* error) +{ + List* list = calloc(1, sizeof(List)); + if (list == NULL) + { + *error = INSUFFICIENT_MEMORY; + } + return list; +} + +void deleteList(List* list) +{ + ListElement* position = list->head; + while (position != NULL) + { + list->head = list->head->next; + free(position->value); + free(position); + position = list->head; + } + free(list); +} + +void deletePosition(Position* position) +{ + free(position); +} + +void removeFirstElement(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + if (list->head == NULL) + { + *error = EMPTY_LIST; + return; + } + if (list->head == list->tail) + { + list->size = 0; + free(list->head->value); + free(list->head); + list->head = NULL; + list->tail = NULL; + return; + } + ListElement* element = list->head; + list->head = list->head->next; + list->size--; + free(element->value); + free(element); + +} + +Position* first(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionFirst = malloc(sizeof(Position)); + if (positionFirst == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionFirst->position = list->head; + return positionFirst; +} + +Position* last(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return NULL; + } + Position* positionLast = malloc(sizeof(Position)); + if (positionLast == NULL) + { + *error = EMPTY_LIST; + return NULL; + } + positionLast->position = list->tail; + return positionLast; +} + +Position* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +bool isLastElement(Position* position) +{ + return position->position->next == NULL; +} + +int numberOfElements(List* list) +{ + return list->size; +} + +char* getHeadValue(List* list) +{ + if (list->head == NULL) + { + return NULL; + } + return list->head->value; +} + +void add(List* list, const char* value, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + if (list->head == NULL) + { + char* valueCopy = calloc(strlen(value) + 1, sizeof(char)); + if (valueCopy == NULL) + { + *error = INSUFFICIENT_MEMORY; + return; + } + strcpy(valueCopy, value); + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + free(valueCopy); + *error = INSUFFICIENT_MEMORY; + return; + } + newElement->value = valueCopy; + list->size = 1; + list->head = newElement; + list->tail = newElement; + list->head->numberOfDuplicateValues = 1; + return; + } + ListElement* head = list->head; + while (head != NULL) + { + if (strcmp(value, head->value) == 0) + { + head->numberOfDuplicateValues++; + *error = ELEMENT_REPEATS; + return; + } + head = head->next; + } + if (head == NULL) + { + char* valueCopy = calloc(strlen(value) + 1, sizeof(char)); + if (valueCopy == NULL) + { + *error = INSUFFICIENT_MEMORY; + return; + } + strcpy(valueCopy, value); + ListElement* newElement = calloc(1, sizeof(ListElement)); + if (newElement == NULL) + { + free(valueCopy); + *error = INSUFFICIENT_MEMORY; + return; + } + newElement->value = valueCopy; + list->size++; + list->tail->next = newElement; + list->tail = list->tail->next; + newElement->numberOfDuplicateValues = 1; + } +} + +char* getValue(Position* position) +{ + return position->position->value; +} + +bool isEmpty(List* list) +{ + return list->head == NULL; +} + +bool isOneElement(List* list) +{ + return list->head->next == NULL; +} + +void print(List* list) +{ + ListElement* element = list->head; + while (element != NULL) + { + printf("%s - %d\n", element->value, element->numberOfDuplicateValues); + element = element->next; + } +} + +const char* decodingError(Error error) +{ + if (error == EMPTY_LIST) + { + return "Error in the program"; + } + if (error == INSUFFICIENT_MEMORY) + { + return "Memory not allocated"; + } + return NULL; +} + +bool inList(List* list, const char* value) +{ + ListElement* element = list->head; + while (element != NULL) + { + if (strcmp(value, element->value) == 0) + { + return true; + } + element = element->next; + } + return false; +} \ No newline at end of file diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h new file mode 100644 index 0000000..e77813e --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h @@ -0,0 +1,59 @@ +#pragma once +#include + +// Structure that represents list +typedef struct List List; + +// This is a structure describing the position of an item in the list. +typedef struct Position Position; + +// Enum type for working with errors +typedef enum Error +{ + NOT_ERROR, + EMPTY_LIST, + INSUFFICIENT_MEMORY, + ELEMENT_REPEATS +} Error; + +// Function for creating a list +List* createList(Error* error); + +// Function for deleting a list +void deleteList(List* list); + +// Function for freeing up memory +void deletePosition(Position* position); + +// Function for adding an item to a list +void add(List* list, const char* value, Error* error); + +// function to find a position to the first element +Position* first(List* list, Error* error); + +// Function for finding a position to the next element +Position* next(Position* position); + +// Function for checking an item for being at the end of the list +bool isLastElement(Position* position); + +// Function for printing a list +void print(List* list); + +// Function for deleting the first element +void removeFirstElement(List* list, Error* error); + +// Function for finding the number of items in the list +int numberOfElements(List* list); + +// Function for checking the emptiness of the list +bool isEmpty(List* list); + +// Function that returns a value for the first element +char* getHeadValue(List* list); + +// Function that returns a value of element +char* getValue(Position* position); + +// Function for error decoding +const char* decodingError(Error error); \ No newline at end of file diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj new file mode 100644 index 0000000..d500241 --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj @@ -0,0 +1,153 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {abf8d20f-cbdb-4353-9bcc-d9ecc7866f3e} + SinglyLinkedListForHashTable + 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;_CRT_SECURE_NO_WARNINGS;_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/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj.filters b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj.filters new file mode 100644 index 0000000..950f937 --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {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/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c new file mode 100644 index 0000000..677718a --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c @@ -0,0 +1,98 @@ +#include "TestSinglyLinkedListForHashTable.h" +#include "SinglyLinkedListForHashTable.h" +#include + +// Function to check the function that adds an item to the list +bool testAdd() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "test", &error); + add(newList, "list", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* firstValue = getHeadValue(newList); + Position* position = first(newList, &error); + if (error == EMPTY_LIST) + { + deleteList(newList); + return false; + } + const char* secondValue = getValue(next(position)); + bool result = strcmp(firstValue, "test") == 0 && strcmp(secondValue, "list") == 0; + deletePosition(position); + deleteList(newList); + return result; +} + +// Function to check the function that deletes the first item in the list +bool testRemoveHead() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "Hello", &error); + add(newList, "World", &error); + removeFirstElement(newList, &error); + if (error == EMPTY_LIST || error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const char* value = getHeadValue(newList); + bool result = strcmp(value, "World") == 0; + deleteList(newList); + return result; +} + +// Function for checking the function counting the number of elements +bool testNumberOfElements() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "Hello", &error); + add(newList, "Merge", &error); + add(newList, "test", &error); + add(newList, "list", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + const int number = numberOfElements(newList); + deleteList(newList); + return number == 4; +} + +// Function that checks the function that returns the value of the first element +bool testGetValue() +{ + Error error = NOT_ERROR; + List* newList = createList(&error); + add(newList, "Hello", &error); + add(newList, "World", &error); + if (error == INSUFFICIENT_MEMORY) + { + deleteList(newList); + return false; + } + Position* position = first(newList, &error); + if (error == EMPTY_LIST) + { + deleteList(newList); + return false; + } + const char* firstValue = getHeadValue(newList); + const char* secondValue = getValue(next(position)); + bool result = strcmp(firstValue, "Hello") == 0 && strcmp(secondValue, "World") == 0; + deleteList(newList); + deletePosition(position); + return result; +} + +bool allTest() +{ + return testAdd() && testGetValue() && testNumberOfElements() && testRemoveHead(); +} \ No newline at end of file diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.h b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.h new file mode 100644 index 0000000..737bfba --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.h @@ -0,0 +1,5 @@ +#pragma once +#include + +// A function that combines the results of all tests +bool allTest(); \ No newline at end of file From 0a7f1609b2303c3171b479877ff97354b6be1c29 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 27 Nov 2021 20:44:18 +0300 Subject: [PATCH 17/24] added a function that determines whether an item is in the list --- .../SinglyLinkedListForHashTable.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h index e77813e..f7feae8 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h @@ -56,4 +56,7 @@ char* getHeadValue(List* list); char* getValue(Position* position); // Function for error decoding -const char* decodingError(Error error); \ No newline at end of file +const char* decodingError(Error error); + +// Function to check whether an item is in the list +bool inList(List* list, const char* value); \ No newline at end of file From 95cf48bd8e7c295354b99529872901e403c65693 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sat, 27 Nov 2021 20:55:53 +0300 Subject: [PATCH 18/24] Revert "added a function that determines whether an item is in the list" This reverts commit 0a7f1609b2303c3171b479877ff97354b6be1c29. --- .../SinglyLinkedListForHashTable.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h index f7feae8..e77813e 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h @@ -56,7 +56,4 @@ char* getHeadValue(List* list); char* getValue(Position* position); // Function for error decoding -const char* decodingError(Error error); - -// Function to check whether an item is in the list -bool inList(List* list, const char* value); \ No newline at end of file +const char* decodingError(Error error); \ No newline at end of file From ff784e0096564d4835744c9e66932b03085732d7 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 5 Dec 2021 13:29:50 +0300 Subject: [PATCH 19/24] changed the function for deleting an element --- .../SinglyLinkedListForHashTable.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c index fd6d1cf..8e5d99e 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c @@ -75,6 +75,10 @@ void removeFirstElement(List* list, Error* error) return; } ListElement* element = list->head; + if (element->numberOfDuplicateValues > 1) + { + element->numberOfDuplicateValues--; + } list->head = list->head->next; list->size--; free(element->value); @@ -168,18 +172,18 @@ void add(List* list, const char* value, Error* error) list->head->numberOfDuplicateValues = 1; return; } - ListElement* head = list->head; - while (head != NULL) + ListElement* element = list->head; + while (element != NULL) { - if (strcmp(value, head->value) == 0) + if (strcmp(value, element->value) == 0) { - head->numberOfDuplicateValues++; + element->numberOfDuplicateValues++; *error = ELEMENT_REPEATS; return; } - head = head->next; + element = element->next; } - if (head == NULL) + if (element == NULL) { char* valueCopy = calloc(strlen(value) + 1, sizeof(char)); if (valueCopy == NULL) From 152c6aa44678354df0fc9e31baff6a45346c49da Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 5 Dec 2021 13:55:54 +0300 Subject: [PATCH 20/24] changed the function for deleting an element --- .../SinglyLinkedListForHashTable.c | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c index 8e5d99e..107b75b 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c @@ -1,3 +1,4 @@ +#define _CRT_SECURE_NO_WARNINGS #include "SinglyLinkedListForHashTable.h" #include #include @@ -9,7 +10,6 @@ typedef struct List { int size; struct ListElement* head; - struct ListElement* tail; } List; // Structure containing a pointer to the next list item and a value variable for the list items @@ -54,6 +54,11 @@ void deletePosition(Position* position) free(position); } +bool isOneElement(List* list) +{ + return list->head->next == NULL; +} + void removeFirstElement(List* list, Error* error) { if (*error != NOT_ERROR) @@ -65,19 +70,19 @@ void removeFirstElement(List* list, Error* error) *error = EMPTY_LIST; return; } - if (list->head == list->tail) + if (isOneElement(list)) { list->size = 0; free(list->head->value); free(list->head); list->head = NULL; - list->tail = NULL; return; } ListElement* element = list->head; if (element->numberOfDuplicateValues > 1) { element->numberOfDuplicateValues--; + return; } list->head = list->head->next; list->size--; @@ -102,22 +107,6 @@ Position* first(List* list, Error* error) return positionFirst; } -Position* last(List* list, Error* error) -{ - if (*error != NOT_ERROR) - { - return NULL; - } - Position* positionLast = malloc(sizeof(Position)); - if (positionLast == NULL) - { - *error = EMPTY_LIST; - return NULL; - } - positionLast->position = list->tail; - return positionLast; -} - Position* next(Position* position) { position->position = position->position->next; @@ -168,7 +157,6 @@ void add(List* list, const char* value, Error* error) newElement->value = valueCopy; list->size = 1; list->head = newElement; - list->tail = newElement; list->head->numberOfDuplicateValues = 1; return; } @@ -200,9 +188,9 @@ void add(List* list, const char* value, Error* error) return; } newElement->value = valueCopy; + newElement->next = list->head; + list->head = newElement; list->size++; - list->tail->next = newElement; - list->tail = list->tail->next; newElement->numberOfDuplicateValues = 1; } } @@ -217,11 +205,6 @@ bool isEmpty(List* list) return list->head == NULL; } -bool isOneElement(List* list) -{ - return list->head->next == NULL; -} - void print(List* list) { ListElement* element = list->head; From b2733c6e0ef27d0df7a7076f51a8ac4ed139f95b Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 5 Dec 2021 14:18:57 +0300 Subject: [PATCH 21/24] adding functions --- .../SinglyLinkedListForHashTable.c | 14 ++++++++++++++ .../SinglyLinkedListForHashTable.h | 8 +++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c index 107b75b..2c31e91 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c @@ -240,4 +240,18 @@ bool inList(List* list, const char* value) element = element->next; } return false; +} + +int returnNumberOfDuplicateValues(List* list, const char* value) +{ + ListElement* element = list->head; + while (element != NULL) + { + if (strcmp(value, element->value) == 0) + { + return element->numberOfDuplicateValues; + } + element = element->next; + } + return 0; } \ No newline at end of file diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h index e77813e..486e9ee 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h @@ -56,4 +56,10 @@ char* getHeadValue(List* list); char* getValue(Position* position); // Function for error decoding -const char* decodingError(Error error); \ No newline at end of file +const char* decodingError(Error error); + +// Function for determining the location of an item in the list +bool inList(List* list, const char* value); + +// Function for counting the number of duplicate elements +int returnNumberOfDuplicateValues(List* list, const char* value); \ No newline at end of file From 6407802e58f1de8ac6bb6f22f9636ef1209fcb60 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Sun, 5 Dec 2021 15:35:26 +0300 Subject: [PATCH 22/24] changing tests --- .../TestSinglyLinkedListForHashTable.c | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c index 677718a..d6d550c 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c @@ -8,22 +8,10 @@ bool testAdd() Error error = NOT_ERROR; List* newList = createList(&error); add(newList, "test", &error); - add(newList, "list", &error); - if (error == INSUFFICIENT_MEMORY) - { - deleteList(newList); - return false; - } const char* firstValue = getHeadValue(newList); - Position* position = first(newList, &error); - if (error == EMPTY_LIST) - { - deleteList(newList); - return false; - } - const char* secondValue = getValue(next(position)); + add(newList, "list", &error); + const char* secondValue = getHeadValue(newList); bool result = strcmp(firstValue, "test") == 0 && strcmp(secondValue, "list") == 0; - deletePosition(position); deleteList(newList); return result; } @@ -42,7 +30,7 @@ bool testRemoveHead() return false; } const char* value = getHeadValue(newList); - bool result = strcmp(value, "World") == 0; + bool result = strcmp(value, "Hello") == 0; deleteList(newList); return result; } @@ -54,8 +42,8 @@ bool testNumberOfElements() List* newList = createList(&error); add(newList, "Hello", &error); add(newList, "Merge", &error); - add(newList, "test", &error); - add(newList, "list", &error); + add(newList, "test", &error); + add(newList, "list", &error); if (error == INSUFFICIENT_MEMORY) { deleteList(newList); @@ -86,7 +74,7 @@ bool testGetValue() } const char* firstValue = getHeadValue(newList); const char* secondValue = getValue(next(position)); - bool result = strcmp(firstValue, "Hello") == 0 && strcmp(secondValue, "World") == 0; + bool result = strcmp(firstValue, "World") == 0 && strcmp(secondValue, "Hello") == 0; deleteList(newList); deletePosition(position); return result; @@ -94,5 +82,5 @@ bool testGetValue() bool allTest() { - return testAdd() && testGetValue() && testNumberOfElements() && testRemoveHead(); + return testAdd() && testRemoveHead() && testNumberOfElements() && testGetValue(); } \ No newline at end of file From a9c8916bab78d62a28dac63df90c32df8bb9405c Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 8 Dec 2021 12:03:33 +0300 Subject: [PATCH 23/24] changing the function to remove an item from the list --- .../SinglyLinkedListForHashTable.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c index 2c31e91..1e73ecd 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c @@ -70,7 +70,7 @@ void removeFirstElement(List* list, Error* error) *error = EMPTY_LIST; return; } - if (isOneElement(list)) + if (isOneElement(list) && list->head->numberOfDuplicateValues == 1) { list->size = 0; free(list->head->value); @@ -113,11 +113,6 @@ Position* next(Position* position) return position; } -bool isLastElement(Position* position) -{ - return position->position->next == NULL; -} - int numberOfElements(List* list) { return list->size; From 8685dee261a7e428bf45ed31a020595a3c8445c7 Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Wed, 8 Dec 2021 15:30:20 +0300 Subject: [PATCH 24/24] removed unnecessary functions --- .../SinglyLinkedListForHashTable.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h index 486e9ee..118b596 100644 --- a/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h @@ -34,9 +34,6 @@ Position* first(List* list, Error* error); // Function for finding a position to the next element Position* next(Position* position); -// Function for checking an item for being at the end of the list -bool isLastElement(Position* position); - // Function for printing a list void print(List* list);