From 1848e05d465121e988ef3f62ea5b7ddc3e60b821 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 30 Nov 2022 13:54:47 +0300 Subject: [PATCH 1/7] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D0=BF=D0=BE=D0=B4=20?= =?UTF-8?q?=D1=85=D1=8D=D1=88-=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashTable/hashTable.sln | 31 ++++ hashTable/hashTable/hash.c | 0 hashTable/hashTable/hash.h | 0 hashTable/hashTable/hashTable.vcxproj | 155 ++++++++++++++++++ hashTable/hashTable/hashTable.vcxproj.filters | 42 +++++ hashTable/hashTable/list.c | 98 +++++++++++ hashTable/hashTable/list.h | 14 ++ hashTable/hashTable/main.c | 7 + hashTable/hashTable/test.c | 0 hashTable/hashTable/test.h | 0 10 files changed, 347 insertions(+) create mode 100644 hashTable/hashTable.sln create mode 100644 hashTable/hashTable/hash.c create mode 100644 hashTable/hashTable/hash.h create mode 100644 hashTable/hashTable/hashTable.vcxproj create mode 100644 hashTable/hashTable/hashTable.vcxproj.filters create mode 100644 hashTable/hashTable/list.c create mode 100644 hashTable/hashTable/list.h create mode 100644 hashTable/hashTable/main.c create mode 100644 hashTable/hashTable/test.c create mode 100644 hashTable/hashTable/test.h diff --git a/hashTable/hashTable.sln b/hashTable/hashTable.sln new file mode 100644 index 0000000..acb8f9e --- /dev/null +++ b/hashTable/hashTable.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hashTable", "hashTable\hashTable.vcxproj", "{95F90EAD-136D-4C34-A865-9E53811D167C}" +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 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Debug|x64.ActiveCfg = Debug|x64 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Debug|x64.Build.0 = Debug|x64 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Debug|x86.ActiveCfg = Debug|Win32 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Debug|x86.Build.0 = Debug|Win32 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Release|x64.ActiveCfg = Release|x64 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Release|x64.Build.0 = Release|x64 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Release|x86.ActiveCfg = Release|Win32 + {95F90EAD-136D-4C34-A865-9E53811D167C}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C8C7A619-4AC6-4277-9DF1-6A284BD7E9D0} + EndGlobalSection +EndGlobal diff --git a/hashTable/hashTable/hash.c b/hashTable/hashTable/hash.c new file mode 100644 index 0000000..e69de29 diff --git a/hashTable/hashTable/hash.h b/hashTable/hashTable/hash.h new file mode 100644 index 0000000..e69de29 diff --git a/hashTable/hashTable/hashTable.vcxproj b/hashTable/hashTable/hashTable.vcxproj new file mode 100644 index 0000000..8a1b9c5 --- /dev/null +++ b/hashTable/hashTable/hashTable.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {95f90ead-136d-4c34-a865-9e53811d167c} + hashTable + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _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/hashTable/hashTable/hashTable.vcxproj.filters b/hashTable/hashTable/hashTable.vcxproj.filters new file mode 100644 index 0000000..3fe2db4 --- /dev/null +++ b/hashTable/hashTable/hashTable.vcxproj.filters @@ -0,0 +1,42 @@ + + + + + {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/hashTable/hashTable/list.c b/hashTable/hashTable/list.c new file mode 100644 index 0000000..637656c --- /dev/null +++ b/hashTable/hashTable/list.c @@ -0,0 +1,98 @@ +#include "list.h" +#include +#include +#include +#include + +typedef struct Node { + char value[100]; + int size; + struct Node* next; +} Node; + +typedef struct List { + struct Node* head; +}; + +Node* newNode(int* errorCode, char value[]) { + Node* temp = calloc(1, sizeof(Node)); + if (temp == NULL) { + *errorCode = -1; + return NULL; + } + strcpy(temp->value, value); + ++temp->size; + return temp; +} + +void insertToList(List* list, char value[], int *errorCode) { + if (list == NULL) { + *errorCode = -1; + return; + } + if (list->head == NULL) { + list->head = newNode(errorCode, value); + return; + } + Node* walker = list->head; + while (walker->next != NULL) { + if (strcmp(walker->value, value) == 0) { + ++walker->size; + return; + } + walker = walker->next; + } + if (strcmp(walker->value, value) == 0) { + ++walker->size; + return; + } + walker->next = newNode(errorCode, value); +} + +Node* helpedClearList(Node* element) { + if (element) { + element = helpedClearList(element->next); + free(element); + return NULL; + } + return NULL; +} + +void clearList(List* list) { + if (list == NULL) { + return; + } + list->head = helpedClearList(list->head); + free(list); + list = NULL; + return; +} + +void deleteNode(List* list, char value[]) { + if (list == NULL || list->head == NULL) { + return; + } + if (strcmp(value, list->head->value) == 0) { + free(list->head); + list->head = NULL; + return; + } + Node* walker = list->head; + while (walker->next != NULL && strcmp(walker->next->value, value) != 0) { + walker = walker->next; + } + if (walker->next != NULL) { + free(walker->next); + walker->next = NULL; + } +} + +List* createList(int *errorCode) { + List* list = calloc(1, sizeof(List)); + if (list == NULL) { + *errorCode = -1; + return NULL; + } + list->head = NULL; + return list; +} \ No newline at end of file diff --git a/hashTable/hashTable/list.h b/hashTable/hashTable/list.h new file mode 100644 index 0000000..ef7bdca --- /dev/null +++ b/hashTable/hashTable/list.h @@ -0,0 +1,14 @@ +#pragma once +typedef struct List List; + +//Add elements to list +void insertToList(List* list, char value[], int* errorCode); + +//Clear list +void clearList(List* list); + +//Delete node in list +void deleteNode(List* list, char value[]); + +//create new list +List* createList(int* errorCode); \ No newline at end of file diff --git a/hashTable/hashTable/main.c b/hashTable/hashTable/main.c new file mode 100644 index 0000000..9aa717a --- /dev/null +++ b/hashTable/hashTable/main.c @@ -0,0 +1,7 @@ +#include "list.h" +#include +#include + +int main() { + +} \ No newline at end of file diff --git a/hashTable/hashTable/test.c b/hashTable/hashTable/test.c new file mode 100644 index 0000000..e69de29 diff --git a/hashTable/hashTable/test.h b/hashTable/hashTable/test.h new file mode 100644 index 0000000..e69de29 From 9fba0a024dc19a94f2a664ddc32fce754ba891ad Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 30 Nov 2022 14:11:24 +0300 Subject: [PATCH 2/7] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20list'a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashTable/hashTable/list.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/hashTable/hashTable/list.c b/hashTable/hashTable/list.c index 637656c..187690a 100644 --- a/hashTable/hashTable/list.c +++ b/hashTable/hashTable/list.c @@ -49,20 +49,18 @@ void insertToList(List* list, char value[], int *errorCode) { walker->next = newNode(errorCode, value); } -Node* helpedClearList(Node* element) { +void helpedClearList(Node* element) { if (element) { - element = helpedClearList(element->next); + helpedClearList(element->next); free(element); - return NULL; } - return NULL; } void clearList(List* list) { if (list == NULL) { return; } - list->head = helpedClearList(list->head); + helpedClearList(list->head); free(list); list = NULL; return; @@ -73,8 +71,9 @@ void deleteNode(List* list, char value[]) { return; } if (strcmp(value, list->head->value) == 0) { + Node* temp = list->head->next; free(list->head); - list->head = NULL; + list->head = temp; return; } Node* walker = list->head; From 9bd33bb131bc0e430de2caaf2b9b06cbd70edcf1 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 30 Nov 2022 23:50:57 +0300 Subject: [PATCH 3/7] =?UTF-8?q?+=D0=A5=D1=8D=D1=88=20=D1=84=D1=83=D0=BD?= =?UTF-8?q?=D0=BA=D1=86=D0=B8=D1=8F,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=8D=D0=BB=D0=BB=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashTable/hashTable/hash.c | 75 +++++++++++++++++++ hashTable/hashTable/hash.h | 12 +++ hashTable/hashTable/hashTable.vcxproj | 3 + hashTable/hashTable/hashTable.vcxproj.filters | 5 ++ hashTable/hashTable/main.c | 40 +++++++++- hashTable/hashTable/text.txt | 1 + 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 hashTable/hashTable/text.txt diff --git a/hashTable/hashTable/hash.c b/hashTable/hashTable/hash.c index e69de29..50fa074 100644 --- a/hashTable/hashTable/hash.c +++ b/hashTable/hashTable/hash.c @@ -0,0 +1,75 @@ +#include "hash.h" +#include "list.h" +#include +#include +#include + +typedef struct ElementsHashTable { + struct List* list; +}; + +typedef struct HashTable { + struct ElementsHashTable** arrayHash; + int hashSize; +}; + +HashTable* createHashArray(int* errorCode, int size) { + HashTable* table = calloc(1, sizeof(HashTable)); + if (table == NULL) { + *errorCode = -1; + return NULL; + } + table->hashSize = size; + table->arrayHash = (ElementsHashTable**)malloc(size * sizeof(ElementsHashTable*)); + if (table->arrayHash == NULL) { + *errorCode = -1; + return; + } + for (int i = 0; i < size; ++i) { + table->arrayHash[i] = (ElementsHashTable*)malloc(1 * sizeof(ElementsHashTable)); + if (table->arrayHash[i] == NULL) { + *errorCode = -1; + return NULL; + } + table->arrayHash[i]->list = NULL; + } + return table; +} + +int fastPow(int number, int degree) { + if (degree < 0) { + return 1.0 / fastPow(number, abs(degree)); + } + if (degree == 0) { + return 1; + } + if (degree % 2 == 0) { + const int copyElement = fastPow(number, degree / 2); + return copyElement * copyElement; + } + return fastPow(number, degree - 1) * number; +} + +int hashFunction(char string[], int size) { + int result = 0; + int simpleNumber = 7; + int neededNumber = fastPow(simpleNumber, (int)strlen(string) - 1); + for (int i = 0; string[i] != '\0'; ++i) { + result += string[i] * neededNumber; + neededNumber /= simpleNumber; + } + return result % size; +} + +void addToHashTable(HashTable* table, char string[], int* errorCode) { + int index = hashFunction(string, table->hashSize); + if (table->arrayHash[index]->list == NULL) { + table->arrayHash[index]->list = createList(errorCode); + if (*errorCode == -1) { + return; + } + insertToList(table->arrayHash[index]->list, string ,errorCode); + } else { + insertToList(table->arrayHash[index]->list, string, errorCode); + } +} \ No newline at end of file diff --git a/hashTable/hashTable/hash.h b/hashTable/hashTable/hash.h index e69de29..ba7e7b2 100644 --- a/hashTable/hashTable/hash.h +++ b/hashTable/hashTable/hash.h @@ -0,0 +1,12 @@ +#pragma once +typedef struct HashTable HashTable; +typedef struct ElementsHashTable ElementsHashTable; + +//Create hashTable +HashTable* createHashArray(int* errorCode, int size); + +//Counting key by string for hash-table +int hashFunction(char string[], int size); + +//Add to hash-table +void addToHashTable(HashTable* table, char string[], int* errorCode); \ No newline at end of file diff --git a/hashTable/hashTable/hashTable.vcxproj b/hashTable/hashTable/hashTable.vcxproj index 8a1b9c5..bf26bd1 100644 --- a/hashTable/hashTable/hashTable.vcxproj +++ b/hashTable/hashTable/hashTable.vcxproj @@ -149,6 +149,9 @@ + + + diff --git a/hashTable/hashTable/hashTable.vcxproj.filters b/hashTable/hashTable/hashTable.vcxproj.filters index 3fe2db4..7912354 100644 --- a/hashTable/hashTable/hashTable.vcxproj.filters +++ b/hashTable/hashTable/hashTable.vcxproj.filters @@ -39,4 +39,9 @@ Исходные файлы + + + Исходные файлы + + \ No newline at end of file diff --git a/hashTable/hashTable/main.c b/hashTable/hashTable/main.c index 9aa717a..eafaf11 100644 --- a/hashTable/hashTable/main.c +++ b/hashTable/hashTable/main.c @@ -1,7 +1,45 @@ #include "list.h" +#include "hash.h" #include #include int main() { - + printf("Enter a more appropriate hash table size\n"); + int size = 0; + int checkScanf = scanf("%d", &size); + while (checkScanf != 1) { + while (getchar() != '\n') { + } + printf("ERROR\n"); + checkScanf = scanf("%d", &size); + } + printf("Print file name with its extension\n"); + char fileName[100] = { '\0' }; + checkScanf = scanf("%s", &fileName); + while (checkScanf != 1) { + while (getchar() != '\n') { + } + printf("ERROR\n"); + checkScanf = scanf("%s", &fileName); + } + int errorCode = 0; + HashTable* table = createHashArray(&errorCode, size); + if (errorCode == -1) { + printf("Memmory error\n"); + return -1; + } + FILE* file = fopen(fileName, "r"); + if (file == NULL) { + printf("Mistake!"); + return -1; + } + char data[100] = { '\0' }; + while (fscanf(file, "%s", data) == 1) { + addToHashTable(table, data, &errorCode); + if (errorCode == -1) { + printf("Memmory error\n"); + return -1; + } + } + fclose(file); } \ No newline at end of file diff --git a/hashTable/hashTable/text.txt b/hashTable/hashTable/text.txt new file mode 100644 index 0000000..af18afa --- /dev/null +++ b/hashTable/hashTable/text.txt @@ -0,0 +1 @@ +abcde rarar avfe ops \ No newline at end of file From 1e6ff0c8445f3ab2909ade453387449072837b42 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 1 Dec 2022 00:09:52 +0300 Subject: [PATCH 4/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BF=D0=B5=D1=87=D0=B0=D1=82=D1=8C=20=D1=8D=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashTable/hashTable/hash.c | 14 ++++++++++++++ hashTable/hashTable/hash.h | 5 ++++- hashTable/hashTable/list.c | 14 ++++++++++++++ hashTable/hashTable/list.h | 5 ++++- hashTable/hashTable/main.c | 1 + 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/hashTable/hashTable/hash.c b/hashTable/hashTable/hash.c index 50fa074..ad93bd3 100644 --- a/hashTable/hashTable/hash.c +++ b/hashTable/hashTable/hash.c @@ -72,4 +72,18 @@ void addToHashTable(HashTable* table, char string[], int* errorCode) { } else { insertToList(table->arrayHash[index]->list, string, errorCode); } +} + +void printHashTable(HashTable* table) { + if (table == NULL || table->arrayHash == NULL) { + return; + } + for (int i = 0; i < table->hashSize; ++i) { + if (table->arrayHash[i]->list != NULL) { + printf("%d)", i); + printList(table->arrayHash[i]->list); + } else { + printf("%d)\n", i); + } + } } \ No newline at end of file diff --git a/hashTable/hashTable/hash.h b/hashTable/hashTable/hash.h index ba7e7b2..e0c1aff 100644 --- a/hashTable/hashTable/hash.h +++ b/hashTable/hashTable/hash.h @@ -9,4 +9,7 @@ HashTable* createHashArray(int* errorCode, int size); int hashFunction(char string[], int size); //Add to hash-table -void addToHashTable(HashTable* table, char string[], int* errorCode); \ No newline at end of file +void addToHashTable(HashTable* table, char string[], int* errorCode); + +//Print hash-table +void printHashTable(HashTable* table); \ No newline at end of file diff --git a/hashTable/hashTable/list.c b/hashTable/hashTable/list.c index 187690a..a59db92 100644 --- a/hashTable/hashTable/list.c +++ b/hashTable/hashTable/list.c @@ -94,4 +94,18 @@ List* createList(int *errorCode) { } list->head = NULL; return list; +} + +void printList(List* list) { + if (list == NULL || list->head == NULL) { + return; + } + Node* walker = list->head; + int i = 0; + while (walker != NULL) { + printf("\n%d: %s -- %d", i, walker->value, walker->size); + walker = walker->next; + ++i; + } + printf("\n"); } \ No newline at end of file diff --git a/hashTable/hashTable/list.h b/hashTable/hashTable/list.h index ef7bdca..5fa2d9e 100644 --- a/hashTable/hashTable/list.h +++ b/hashTable/hashTable/list.h @@ -11,4 +11,7 @@ void clearList(List* list); void deleteNode(List* list, char value[]); //create new list -List* createList(int* errorCode); \ No newline at end of file +List* createList(int* errorCode); + +//Print list +void printList(List* list); \ No newline at end of file diff --git a/hashTable/hashTable/main.c b/hashTable/hashTable/main.c index eafaf11..becc4e5 100644 --- a/hashTable/hashTable/main.c +++ b/hashTable/hashTable/main.c @@ -42,4 +42,5 @@ int main() { } } fclose(file); + printHashTable(table); } \ No newline at end of file From 0a240d3ed68aa404e8f9babd3914a210ad4678e2 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 1 Dec 2022 00:30:44 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=BA=D0=BE=D1=8D=D1=84?= =?UTF-8?q?=D1=84=D0=B8=D1=86=D0=B8=D0=B5=D0=BD=D1=82=D0=B0=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashTable/hashTable/hash.c | 21 +++++++++++++++++++++ hashTable/hashTable/hash.h | 10 ++++++++-- hashTable/hashTable/main.c | 4 +++- hashTable/hashTable/text.txt | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/hashTable/hashTable/hash.c b/hashTable/hashTable/hash.c index ad93bd3..b5315c1 100644 --- a/hashTable/hashTable/hash.c +++ b/hashTable/hashTable/hash.c @@ -86,4 +86,25 @@ void printHashTable(HashTable* table) { printf("%d)\n", i); } } +} + +void clearHashTable(HashTable* table) { + for (int i = 0; i < table->hashSize; ++i) { + if (table->arrayHash[i]->list != NULL) { + clearList(table->arrayHash[i]->list); + } + } + free(table->arrayHash); + free(table); + table = NULL; +} + +double fillFactor(HashTable* table) { + double busy = 0; + for (int i = 0; i < table->hashSize; ++i) { + if (table->arrayHash[i]->list != NULL) { + ++busy; + } + } + return busy == 0 ? -1 : busy / (double)table->hashSize; } \ No newline at end of file diff --git a/hashTable/hashTable/hash.h b/hashTable/hashTable/hash.h index e0c1aff..01944dd 100644 --- a/hashTable/hashTable/hash.h +++ b/hashTable/hashTable/hash.h @@ -2,7 +2,7 @@ typedef struct HashTable HashTable; typedef struct ElementsHashTable ElementsHashTable; -//Create hashTable +//Create hash-table HashTable* createHashArray(int* errorCode, int size); //Counting key by string for hash-table @@ -12,4 +12,10 @@ int hashFunction(char string[], int size); void addToHashTable(HashTable* table, char string[], int* errorCode); //Print hash-table -void printHashTable(HashTable* table); \ No newline at end of file +void printHashTable(HashTable* table); + +//Clear hash-table +void clearHashTable(HashTable* table); + +//find fill factor hash-table +double fillFactor(HashTable* table); \ No newline at end of file diff --git a/hashTable/hashTable/main.c b/hashTable/hashTable/main.c index becc4e5..d5a3100 100644 --- a/hashTable/hashTable/main.c +++ b/hashTable/hashTable/main.c @@ -13,7 +13,7 @@ int main() { printf("ERROR\n"); checkScanf = scanf("%d", &size); } - printf("Print file name with its extension\n"); + printf("Print file name with its extension <100 symbols\n"); char fileName[100] = { '\0' }; checkScanf = scanf("%s", &fileName); while (checkScanf != 1) { @@ -43,4 +43,6 @@ int main() { } fclose(file); printHashTable(table); + printf("%f", fillFactor(table)); + clearHashTable(table); } \ No newline at end of file diff --git a/hashTable/hashTable/text.txt b/hashTable/hashTable/text.txt index af18afa..5e7ad8b 100644 --- a/hashTable/hashTable/text.txt +++ b/hashTable/hashTable/text.txt @@ -1 +1 @@ -abcde rarar avfe ops \ No newline at end of file +Calculate the frequency of occurrence of words in the text using a hash table. At the input of the text file, output to the console all the words that occur in this text with the number of times that each word occurs. A word is a sequence of characters separated by spaces, different word forms are considered different words. Implement the hash table in a separate module using the "List" module. Calculate and output also the hash table fill factor, the maximum and average length of the list in the table segment. The number of "free" attempts to surrender \ No newline at end of file From 88a1971f364128f72c2e186e3b4448ee1288b9f7 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 1 Dec 2022 01:12:44 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashTable/hashTable/hash.c | 21 +++++++++++++++++++++ hashTable/hashTable/hash.h | 6 +++++- hashTable/hashTable/list.c | 14 ++++++++++++++ hashTable/hashTable/list.h | 7 ++++++- hashTable/hashTable/main.c | 7 +++++++ hashTable/hashTable/test.c | 35 +++++++++++++++++++++++++++++++++++ hashTable/hashTable/test.h | 5 +++++ 7 files changed, 93 insertions(+), 2 deletions(-) diff --git a/hashTable/hashTable/hash.c b/hashTable/hashTable/hash.c index b5315c1..0d2ad09 100644 --- a/hashTable/hashTable/hash.c +++ b/hashTable/hashTable/hash.c @@ -3,6 +3,7 @@ #include #include #include +#include typedef struct ElementsHashTable { struct List* list; @@ -107,4 +108,24 @@ double fillFactor(HashTable* table) { } } return busy == 0 ? -1 : busy / (double)table->hashSize; +} + +bool ifAllInTableWithoutIdentical(HashTable* table, char** buffer, int sizeBuffer) { + if (table == NULL) { + if (buffer == NULL) { + return true; + } + return false; + } + int howMuchInTable = 0; + for (int i = 0; i < table->hashSize; ++i) { + if (table->arrayHash[i]->list != NULL) { + for (int j = 0; j < sizeBuffer; ++j) { + if (findStringInList(table->arrayHash[i]->list, buffer[j])) { + ++howMuchInTable; + } + } + } + } + return howMuchInTable == sizeBuffer; } \ No newline at end of file diff --git a/hashTable/hashTable/hash.h b/hashTable/hashTable/hash.h index 01944dd..2ea278d 100644 --- a/hashTable/hashTable/hash.h +++ b/hashTable/hashTable/hash.h @@ -1,4 +1,5 @@ #pragma once +#include typedef struct HashTable HashTable; typedef struct ElementsHashTable ElementsHashTable; @@ -18,4 +19,7 @@ void printHashTable(HashTable* table); void clearHashTable(HashTable* table); //find fill factor hash-table -double fillFactor(HashTable* table); \ No newline at end of file +double fillFactor(HashTable* table); + +//Only for test +bool ifAllInTableWithoutIdentical(HashTable* table, char** buffer, int sizeBuffer); \ No newline at end of file diff --git a/hashTable/hashTable/list.c b/hashTable/hashTable/list.c index a59db92..20c2c03 100644 --- a/hashTable/hashTable/list.c +++ b/hashTable/hashTable/list.c @@ -108,4 +108,18 @@ void printList(List* list) { ++i; } printf("\n"); +} + +bool findStringInList(List* list, char value[]) { + if (list == NULL) { + return false; + } + Node* walker = list->head; + while (walker != NULL) { + if (strcmp(walker->value, value) == 0) { + return true; + } + walker = walker->next; + } + return false; } \ No newline at end of file diff --git a/hashTable/hashTable/list.h b/hashTable/hashTable/list.h index 5fa2d9e..151fb5e 100644 --- a/hashTable/hashTable/list.h +++ b/hashTable/hashTable/list.h @@ -1,4 +1,6 @@ #pragma once +#include + typedef struct List List; //Add elements to list @@ -14,4 +16,7 @@ void deleteNode(List* list, char value[]); List* createList(int* errorCode); //Print list -void printList(List* list); \ No newline at end of file +void printList(List* list); + +//Is string in List +bool findStringInList(List* list, char value[]); \ No newline at end of file diff --git a/hashTable/hashTable/main.c b/hashTable/hashTable/main.c index d5a3100..55c6c9d 100644 --- a/hashTable/hashTable/main.c +++ b/hashTable/hashTable/main.c @@ -1,9 +1,16 @@ #include "list.h" #include "hash.h" +#include "test.h" #include #include int main() { + if (test()) { + printf("Test correct\n"); + } else { + printf("Problems...\n"); + return -1; + } printf("Enter a more appropriate hash table size\n"); int size = 0; int checkScanf = scanf("%d", &size); diff --git a/hashTable/hashTable/test.c b/hashTable/hashTable/test.c index e69de29..1592f6c 100644 --- a/hashTable/hashTable/test.c +++ b/hashTable/hashTable/test.c @@ -0,0 +1,35 @@ +#include "hash.h" +#include +#include +#include + +bool test(void) { + int errorCode = 0; + HashTable* table = createHashArray(&errorCode, 4); + if (errorCode == -1) { + return false; + } + if (table == NULL) { + return false; + } + char bufferFirst[4] = { 'a', 'b', 'c', '\0'}; + char bufferSecond[4] = { 'a', 'c', 'b', '\0'}; + addToHashTable(table, bufferFirst, &errorCode); + if (errorCode == -1) { + return false; + } + addToHashTable(table, bufferSecond, &errorCode); + if (errorCode == -1) { + return false; + } + char** buffer = (char**)malloc(2 * sizeof(char*)); + if (buffer == NULL) { + return false; + } + buffer[0] = bufferFirst; + buffer[1] = bufferSecond; + bool testResult = ifAllInTableWithoutIdentical(table, buffer, 2); + clearHashTable(table); + free(buffer); + return testResult; +} \ No newline at end of file diff --git a/hashTable/hashTable/test.h b/hashTable/hashTable/test.h index e69de29..de74744 100644 --- a/hashTable/hashTable/test.h +++ b/hashTable/hashTable/test.h @@ -0,0 +1,5 @@ +#pragma once +#include + +//Test for hash-table +bool test(void); \ No newline at end of file From 534471ffe48e589e1037e972980fa6b82edf770d Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 23 Dec 2022 17:53:47 +0300 Subject: [PATCH 7/7] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hashTable/hashTable/hash.c | 13 +++++++------ hashTable/hashTable/hash.h | 5 +---- hashTable/hashTable/test.c | 4 +++- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/hashTable/hashTable/hash.c b/hashTable/hashTable/hash.c index 0d2ad09..b121148 100644 --- a/hashTable/hashTable/hash.c +++ b/hashTable/hashTable/hash.c @@ -7,9 +7,9 @@ typedef struct ElementsHashTable { struct List* list; -}; +} ElementsHashTable; -typedef struct HashTable { +struct HashTable { struct ElementsHashTable** arrayHash; int hashSize; }; @@ -29,6 +29,9 @@ HashTable* createHashArray(int* errorCode, int size) { for (int i = 0; i < size; ++i) { table->arrayHash[i] = (ElementsHashTable*)malloc(1 * sizeof(ElementsHashTable)); if (table->arrayHash[i] == NULL) { + for (int j = 0; j < i; ++j) { + free(table->arrayHash[j]); + } *errorCode = -1; return NULL; } @@ -59,7 +62,7 @@ int hashFunction(char string[], int size) { result += string[i] * neededNumber; neededNumber /= simpleNumber; } - return result % size; + return abs(result) % size; } void addToHashTable(HashTable* table, char string[], int* errorCode) { @@ -69,10 +72,8 @@ void addToHashTable(HashTable* table, char string[], int* errorCode) { if (*errorCode == -1) { return; } - insertToList(table->arrayHash[index]->list, string ,errorCode); - } else { - insertToList(table->arrayHash[index]->list, string, errorCode); } + insertToList(table->arrayHash[index]->list, string, errorCode); } void printHashTable(HashTable* table) { diff --git a/hashTable/hashTable/hash.h b/hashTable/hashTable/hash.h index 2ea278d..22788cb 100644 --- a/hashTable/hashTable/hash.h +++ b/hashTable/hashTable/hash.h @@ -1,14 +1,11 @@ #pragma once #include + typedef struct HashTable HashTable; -typedef struct ElementsHashTable ElementsHashTable; //Create hash-table HashTable* createHashArray(int* errorCode, int size); -//Counting key by string for hash-table -int hashFunction(char string[], int size); - //Add to hash-table void addToHashTable(HashTable* table, char string[], int* errorCode); diff --git a/hashTable/hashTable/test.c b/hashTable/hashTable/test.c index 1592f6c..5d75fe7 100644 --- a/hashTable/hashTable/test.c +++ b/hashTable/hashTable/test.c @@ -32,4 +32,6 @@ bool test(void) { clearHashTable(table); free(buffer); return testResult; -} \ No newline at end of file +} +//Resize - 2 +//FillFacot - 1 \ No newline at end of file