Skip to content

Commit

Permalink
Refactor HashTable
Browse files Browse the repository at this point in the history
  • Loading branch information
holman57 committed May 11, 2024
1 parent b6a219f commit 8f975b2
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 28 deletions.
40 changes: 20 additions & 20 deletions HashTable/HashTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef struct HashTable

LinkedList* allocate_list(void)
{
LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));
LinkedList* list = (LinkedList*) malloc(sizeof(LinkedList));
return list;
}

Expand Down Expand Up @@ -99,7 +99,7 @@ void free_linkedlist(LinkedList* list)

LinkedList** create_overflow_buckets(HashTable* table)
{
LinkedList** buckets = (LinkedList**)calloc(table->size, sizeof(LinkedList*));
LinkedList** buckets = (LinkedList**) calloc(table->size, sizeof(LinkedList*));
for (int i = 0; i < table->size; i++) buckets[i] = NULL;
return buckets;
}
Expand All @@ -113,17 +113,17 @@ void free_overflow_buckets(HashTable* table)

Ht_item* create_item(char* key, char* value)
{
Ht_item* item = (Ht_item*)malloc(sizeof(Ht_item));
item->key = (char*)malloc(strlen(key) + 1);
item->value = (char*)malloc(strlen(value) + 1);
Ht_item* item = (Ht_item*) malloc(sizeof(Ht_item));
item->key = (char*) malloc(strlen(key) + 1);
item->value = (char*) malloc(strlen(value) + 1);
strcpy(item->key, key);
strcpy(item->value, value);
return item;
}

HashTable* create_table(int size)
{
HashTable* table = (HashTable*)malloc(sizeof(HashTable));
HashTable* table = (HashTable*) malloc(sizeof(HashTable));
table->size = size;
table->count = 0;
table->items = (Ht_item**)calloc(table->size, sizeof(Ht_item*));
Expand Down Expand Up @@ -171,7 +171,7 @@ void handle_collision(HashTable* table, unsigned long index, Ht_item* item)
void ht_insert(HashTable* table, char* key, char* value)
{
Ht_item* item = create_item(key, value);
int index = (int)hash_function(key);
int index = (int) hash_function(key);
Ht_item* current_item = table->items[index];
if (current_item == NULL)
{
Expand Down Expand Up @@ -201,7 +201,7 @@ void ht_insert(HashTable* table, char* key, char* value)

char* ht_search(HashTable* table, char* key)
{
int index = (int)hash_function(key);
int index = (int) hash_function(key);
Ht_item* item = table->items[index];
LinkedList* head = table->overflow_buckets[index];
if (item != NULL)
Expand All @@ -216,7 +216,7 @@ char* ht_search(HashTable* table, char* key)

void ht_delete(HashTable* table, char* key)
{
int index = (int)hash_function(key);
int index = (int) hash_function(key);
Ht_item* item = table->items[index];
LinkedList* head = table->overflow_buckets[index];
if (item == NULL)
Expand Down Expand Up @@ -303,18 +303,18 @@ void print_table(HashTable* table)
int main(void)
{
HashTable* ht = create_table(CAPACITY);
ht_insert(ht, (char*)"1", (char*)"First address");
ht_insert(ht, (char*)"2", (char*)"Second address");
ht_insert(ht, (char*)"Hel", (char*)"Third address");
ht_insert(ht, (char*)"Cau", (char*)"Fourth address");
print_search(ht, (char*)"1");
print_search(ht, (char*)"2");
print_search(ht, (char*)"3");
print_search(ht, (char*)"Hel");
print_search(ht, (char*)"Cau");
ht_insert(ht, (char*) "1", (char*) "First address");
ht_insert(ht, (char*) "2", (char*) "Second address");
ht_insert(ht, (char*) "Hel", (char*) "Third address");
ht_insert(ht, (char*) "Cau", (char*) "Fourth address");
print_search(ht, (char*) "1");
print_search(ht, (char*) "2");
print_search(ht, (char*) "3");
print_search(ht, (char*) "Hel");
print_search(ht, (char*) "Cau");
print_table(ht);
ht_delete(ht, (char*)"1");
ht_delete(ht, (char*)"Cau");
ht_delete(ht, (char*) "1");
ht_delete(ht, (char*) "Cau");
print_table(ht);
free_table(ht);
return 0;
Expand Down
Binary file added HashTable/x64/Debug/HashTable.exe
Binary file not shown.
11 changes: 11 additions & 0 deletions HashTable/x64/Debug/HashTable.exe.recipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ProjectOutputs>
<ProjectOutput>
<FullPath>C:\Users\LukeH\Desktop\C-Examples\HashTable\x64\Debug\HashTable.exe</FullPath>
</ProjectOutput>
</ProjectOutputs>
<ContentFiles />
<SatelliteDlls />
<NonRecipeFileRefs />
</Project>
Binary file added HashTable/x64/Debug/HashTable.ilk
Binary file not shown.
2 changes: 2 additions & 0 deletions HashTable/x64/Debug/HashTable.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
 HashTable.c
HashTable.vcxproj -> C:\Users\LukeH\Desktop\C-Examples\HashTable\x64\Debug\HashTable.exe
Binary file added HashTable/x64/Debug/HashTable.obj
Binary file not shown.
Binary file added HashTable/x64/Debug/HashTable.pdb
Binary file not shown.
Binary file added HashTable/x64/Debug/HashTable.tlog/CL.command.1.tlog
Binary file not shown.
Binary file added HashTable/x64/Debug/HashTable.tlog/CL.read.1.tlog
Binary file not shown.
Binary file added HashTable/x64/Debug/HashTable.tlog/CL.write.1.tlog
Binary file not shown.
1 change: 1 addition & 0 deletions HashTable/x64/Debug/HashTable.tlog/Cl.items.tlog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:\Users\LukeH\Desktop\C-Examples\HashTable\HashTable.c;C:\Users\LukeH\Desktop\C-Examples\HashTable\x64\Debug\HashTable.obj
2 changes: 2 additions & 0 deletions HashTable/x64/Debug/HashTable.tlog/HashTable.lastbuildstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.36.32532:TargetPlatformVersion=10.0.22000.0:
Debug|x64|C:\Users\LukeH\Desktop\C-Examples\HashTable\|
Binary file not shown.
Binary file not shown.
Binary file added HashTable/x64/Debug/HashTable.tlog/link.write.1.tlog
Binary file not shown.
1 change: 1 addition & 0 deletions HashTable/x64/Debug/HashTable.vcxproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:\Users\LukeH\Desktop\C-Examples\HashTable\x64\Debug\HashTable.exe
Binary file added HashTable/x64/Debug/vc143.idb
Binary file not shown.
Binary file added HashTable/x64/Debug/vc143.pdb
Binary file not shown.
16 changes: 8 additions & 8 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct HashTable

LinkedList *allocate_list(void)
{
LinkedList *list = (LinkedList *)malloc(sizeof(LinkedList));
LinkedList *list = (LinkedList *) malloc(sizeof(LinkedList));
return list;
}

Expand Down Expand Up @@ -97,7 +97,7 @@ void free_linkedlist(LinkedList *list)

LinkedList **create_overflow_buckets(HashTable *table)
{
LinkedList **buckets = (LinkedList **)calloc(table->size, sizeof(LinkedList *));
LinkedList **buckets = (LinkedList **) calloc(table->size, sizeof(LinkedList *));
for (int i = 0; i < table->size; i++) buckets[i] = NULL;
return buckets;
}
Expand All @@ -111,20 +111,20 @@ void free_overflow_buckets(HashTable *table)

Ht_item *create_item(char *key, char *value)
{
Ht_item *item = (Ht_item *)malloc(sizeof(Ht_item));
item->key = (char *)malloc(strlen(key) + 1);
item->value = (char *)malloc(strlen(value) + 1);
Ht_item *item = (Ht_item *) malloc(sizeof(Ht_item));
item->key = (char *) malloc(strlen(key) + 1);
item->value = (char *) malloc(strlen(value) + 1);
strcpy(item->key, key);
strcpy(item->value, value);
return item;
}

HashTable *create_table(int size)
{
HashTable *table = (HashTable *)malloc(sizeof(HashTable));
HashTable *table = (HashTable *) malloc(sizeof(HashTable));
table->size = size;
table->count = 0;
table->items = (Ht_item **)calloc(table->size, sizeof(Ht_item *));
table->items = (Ht_item **) calloc(table->size, sizeof(Ht_item *));
for (int i = 0; i < table->size; i++) table->items[i] = NULL;
table->overflow_buckets = create_overflow_buckets(table);
return table;
Expand Down Expand Up @@ -292,7 +292,7 @@ void print_table(HashTable *table)
{
if (table -> items[i])
{
printf("Index:%d, Key:%s, Value:%s\n", i, table -> items[i] -> key, table -> items[i] -> value);
printf("Index:%d, Key:%s, Value:%s\n", i, table->items[i]->key, table->items[i]->value);
}
}
printf("-------------------\n\n");
Expand Down

0 comments on commit 8f975b2

Please sign in to comment.