From e25ca37949aceaa3e3cdec5dd11de9d1f2764fe5 Mon Sep 17 00:00:00 2001 From: ArtiomPatov Date: Thu, 2 Dec 2021 23:08:01 +0300 Subject: [PATCH] fixed deleteHashTable function bug --- hashTable/hashTable/hashTable.c | 28 ++++++++++++++-------------- hashTable/hashTable/hashTable.h | 20 ++++++++++---------- hashTable/hashTable/hashTableTests.c | 7 ++++--- hashTable/hashTable/list.h | 4 ++++ hashTable/hashTable/main.c | 2 +- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/hashTable/hashTable/hashTable.c b/hashTable/hashTable/hashTable.c index c550e45..3509bfe 100644 --- a/hashTable/hashTable/hashTable.c +++ b/hashTable/hashTable/hashTable.c @@ -6,16 +6,16 @@ #include "hashTable.h" #include "list.h" -typedef struct hashTable +typedef struct HashTable { List** array; int numberOfElements; int arraySize; -} hashTable; +} HashTable; -hashTable* createHashTable() +HashTable* createHashTable() { - hashTable* hashTable = calloc(1, sizeof(hashTable)); + HashTable* hashTable = calloc(1, sizeof(HashTable)); if (hashTable == NULL) { return NULL; @@ -42,7 +42,7 @@ void deleteArray(List** array, int arraySize) } } -void deleteHashTable(hashTable** hashTable) +void deleteHashTable(HashTable** hashTable) { int arraySize = (*hashTable)->arraySize; for (int index = 0; index < arraySize; index++) @@ -54,7 +54,7 @@ void deleteHashTable(hashTable** hashTable) *hashTable = NULL; } -bool addItemToHashTable(hashTable* hashTable, const char word[]) +bool addItemToHashTable(HashTable* hashTable, const char word[]) { int hash = hashFunction(word, hashTable->arraySize); ++hashTable->numberOfElements; @@ -65,7 +65,7 @@ bool addItemToHashTable(hashTable* hashTable, const char word[]) return true; } -bool resize(hashTable* hashTable) +bool resize(HashTable* hashTable) { int oldSize = hashTable->arraySize; List** newArray = calloc(oldSize * 2, sizeof(List*)); @@ -98,7 +98,7 @@ bool resize(hashTable* hashTable) return true; } -bool parse(hashTable* hashTable, char* fileName) +bool parse(HashTable* hashTable, char* fileName) { FILE* file = fopen(fileName, "r"); while (!feof(file)) @@ -130,14 +130,14 @@ int hashFunction(const char word[], int arraySize) return hash % arraySize; } -int getCounter(hashTable* hashTable, const char word[]) +int getCounter(HashTable* hashTable, const char word[]) { int hash = hashFunction(word, hashTable->arraySize); List* list = hashTable->array[hash]; return getCounterFromList(list, word); } -void printHashTable(hashTable* hashTable) +void printHashTable(HashTable* hashTable) { int arraySize = hashTable->arraySize; for (int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++) @@ -146,7 +146,7 @@ void printHashTable(hashTable* hashTable) } } -int getMaxLength(hashTable* hashTable) +int getMaxLength(HashTable* hashTable) { int maxLength = 0; int arraySize = hashTable->arraySize; @@ -164,7 +164,7 @@ int getMaxLength(hashTable* hashTable) return maxLength; } -int getAverageLength(hashTable* hashTable) +int getAverageLength(HashTable* hashTable) { int sumOfLengths = 0; int arraySize = hashTable->arraySize; @@ -178,12 +178,12 @@ int getAverageLength(hashTable* hashTable) return sumOfLengths / arraySize; } -double getLoadFactor(hashTable* hashTable) +double getLoadFactor(HashTable* hashTable) { return (double)hashTable->numberOfElements / (double)hashTable->arraySize; } -int getNumberOfElements(hashTable* hashTable) +int getNumberOfElements(HashTable* hashTable) { return hashTable->numberOfElements; } \ No newline at end of file diff --git a/hashTable/hashTable/hashTable.h b/hashTable/hashTable/hashTable.h index cb00c44..2819c1b 100644 --- a/hashTable/hashTable/hashTable.h +++ b/hashTable/hashTable/hashTable.h @@ -2,34 +2,34 @@ #include // hash table struct -typedef struct hashTable hashTable; +typedef struct HashTable HashTable; // creates hash table -hashTable* createHashTable(); +HashTable* createHashTable(); // deletes hash table -void deleteHashTable(hashTable** hashTable); +void deleteHashTable(HashTable** hashTable); // parses file -bool parse(hashTable* hashTable, char* fileName); +bool parse(HashTable* hashTable, char* fileName); // counts hash for given word int hashFunction(const char word[], int arraySize); // returns word frequency -int getCounter(hashTable* hashTable, const char word[]); +int getCounter(HashTable* hashTable, const char word[]); // prints all words from hash table and their frequency in console -void printHashTable(hashTable* hashTable); +void printHashTable(HashTable* hashTable); // returns max length of hash table lists -int getMaxLength(hashTable* hashTable); +int getMaxLength(HashTable* hashTable); // returns average length of hash table lists -int getAverageLength(hashTable* hashTable); +int getAverageLength(HashTable* hashTable); // returns load factor of hash table -double getLoadFactor(hashTable* hashTable); +double getLoadFactor(HashTable* hashTable); // returns number of elements in hash table -int getNumberOfElements(hashTable* hashTable); \ No newline at end of file +int getNumberOfElements(HashTable* hashTable); \ No newline at end of file diff --git a/hashTable/hashTable/hashTableTests.c b/hashTable/hashTable/hashTableTests.c index 6af600f..1443f4d 100644 --- a/hashTable/hashTable/hashTableTests.c +++ b/hashTable/hashTable/hashTableTests.c @@ -1,11 +1,12 @@ #include #include +#include #include "hashTable.h" #include "list.h" bool hashTablePassedTests() { - hashTable* hashTable = createHashTable(); + HashTable* hashTable = createHashTable(); if (hashTable == NULL) { return false; @@ -25,8 +26,8 @@ bool hashTablePassedTests() } int averageLength = getAverageLength(hashTable); int maximumLength = getMaxLength(hashTable); - int loadFactor = getLoadFactor(hashTable); + double loadFactor = -getLoadFactor(hashTable); int numberOfElements = getNumberOfElements(hashTable); deleteHashTable(&hashTable); - return hashTable == NULL && averageLength == 0 && maximumLength == 2 && loadFactor == 0 && numberOfElements == 24; + return hashTable == NULL && averageLength == 0 && maximumLength == 2 && loadFactor < 1 && numberOfElements == 24; } \ No newline at end of file diff --git a/hashTable/hashTable/list.h b/hashTable/hashTable/list.h index 75422b0..6a9f6d0 100644 --- a/hashTable/hashTable/list.h +++ b/hashTable/hashTable/list.h @@ -10,6 +10,7 @@ typedef struct Position Position; // allocates memory for list List* createList(); +// creates position Position* createPosition(); // checks if list is empty @@ -18,6 +19,9 @@ bool isEmpty(List* list); // deletes all list elements and list void deleteList(List** list); +// deletes position +void deletePosition(Position** position); + // returns length of the list int getLength(List* list); diff --git a/hashTable/hashTable/main.c b/hashTable/hashTable/main.c index de59691..d0b4a31 100644 --- a/hashTable/hashTable/main.c +++ b/hashTable/hashTable/main.c @@ -10,7 +10,7 @@ int main() printf("Tests failed"); return -1; } - hashTable* myHashTable = createHashTable(); + HashTable* myHashTable = createHashTable(); if (myHashTable == NULL) { printf("Allocation error");