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..e690707 --- /dev/null +++ b/SinglyLinkedList/NewList/NewList/List.c @@ -0,0 +1,262 @@ +#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) +{ + 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..18a8b17 --- /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(); 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..1e73ecd --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.c @@ -0,0 +1,252 @@ +#define _CRT_SECURE_NO_WARNINGS +#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; +} 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); +} + +bool isOneElement(List* list) +{ + return list->head->next == NULL; +} + +void removeFirstElement(List* list, Error* error) +{ + if (*error != NOT_ERROR) + { + return; + } + if (list->head == NULL) + { + *error = EMPTY_LIST; + return; + } + if (isOneElement(list) && list->head->numberOfDuplicateValues == 1) + { + list->size = 0; + free(list->head->value); + free(list->head); + list->head = NULL; + return; + } + ListElement* element = list->head; + if (element->numberOfDuplicateValues > 1) + { + element->numberOfDuplicateValues--; + return; + } + 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* next(Position* position) +{ + position->position = position->position->next; + return position; +} + +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->head->numberOfDuplicateValues = 1; + return; + } + ListElement* element = list->head; + while (element != NULL) + { + if (strcmp(value, element->value) == 0) + { + element->numberOfDuplicateValues++; + *error = ELEMENT_REPEATS; + return; + } + element = element->next; + } + if (element == 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; + newElement->next = list->head; + list->head = newElement; + list->size++; + newElement->numberOfDuplicateValues = 1; + } +} + +char* getValue(Position* position) +{ + return position->position->value; +} + +bool isEmpty(List* list) +{ + return list->head == 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; +} + +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 new file mode 100644 index 0000000..118b596 --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable.h @@ -0,0 +1,62 @@ +#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 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); + +// 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 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..d6d550c --- /dev/null +++ b/SinglyLinkedListForHashTable/SinglyLinkedListForHashTable/TestSinglyLinkedListForHashTable.c @@ -0,0 +1,86 @@ +#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); + const char* firstValue = getHeadValue(newList); + add(newList, "list", &error); + const char* secondValue = getHeadValue(newList); + bool result = strcmp(firstValue, "test") == 0 && strcmp(secondValue, "list") == 0; + 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, "Hello") == 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, "World") == 0 && strcmp(secondValue, "Hello") == 0; + deleteList(newList); + deletePosition(position); + return result; +} + +bool allTest() +{ + return testAdd() && testRemoveHead() && testNumberOfElements() && testGetValue(); +} \ 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