Skip to content

Commit

Permalink
fixed deleteHashTable function bug
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiipatov committed Dec 2, 2021
1 parent 05f0905 commit e25ca37
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
28 changes: 14 additions & 14 deletions hashTable/hashTable/hashTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++)
Expand All @@ -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;
Expand All @@ -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*));
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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++)
Expand All @@ -146,7 +146,7 @@ void printHashTable(hashTable* hashTable)
}
}

int getMaxLength(hashTable* hashTable)
int getMaxLength(HashTable* hashTable)
{
int maxLength = 0;
int arraySize = hashTable->arraySize;
Expand All @@ -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;
Expand All @@ -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;
}
20 changes: 10 additions & 10 deletions hashTable/hashTable/hashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
#include <stdbool.h>

// 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);
int getNumberOfElements(HashTable* hashTable);
7 changes: 4 additions & 3 deletions hashTable/hashTable/hashTableTests.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include "hashTable.h"
#include "list.h"

bool hashTablePassedTests()
{
hashTable* hashTable = createHashTable();
HashTable* hashTable = createHashTable();
if (hashTable == NULL)
{
return false;
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions hashTable/hashTable/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef struct Position Position;
// allocates memory for list
List* createList();

// creates position
Position* createPosition();

// checks if list is empty
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion hashTable/hashTable/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main()
printf("Tests failed");
return -1;
}
hashTable* myHashTable = createHashTable();
HashTable* myHashTable = createHashTable();
if (myHashTable == NULL)
{
printf("Allocation error");
Expand Down

0 comments on commit e25ca37

Please sign in to comment.