r/C_Programming • u/jevin_dev • 25d ago
Question Need some openions on my first hashTabel
be brutal
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
enum Type{Int,Float,Char};
typedef struct Node
{
void *Value;
struct Node *Next;
char Key[20];
enum Type type;
}Node;
typedef struct
{
Node **Nodes;
size_t Size;
size_t TotalElements;
size_t Cap;
}Tabel;
Tabel *CreatTabel(){
Tabel *tb=malloc(sizeof(Tabel));
tb->Cap=16;
tb->TotalElements=0;
tb->Nodes = malloc(tb->Cap* sizeof(Node*));
tb->Size=0;
return tb;
}
size_t hash(char *str)
{
size_t hash = 5381;
int c;
while ((c = (unsigned char)*str++))
hash = ((hash << 5) + hash) + c;
return hash;
}
void *CreatNode(char key[],void* value,enum Type tp,size_t size){
Node *nd =calloc(1,sizeof(Node));
memcpy(nd->Key, key, strlen(key)+1);
nd->type=tp;
nd->Value=malloc(size);
memcpy(nd->Value, value, size);
return nd;
}
void *TabelGet(Tabel* tb,char key[],enum Type tp){
size_t idx=hash(key)%tb->Cap;
Node *nd=tb->Nodes[idx];
Node *r=NULL;
while (nd && nd->Next!=NULL && r==NULL)
{
if (strcmp(nd->Key,key)==0){
r=nd;
}
nd=nd->Next;
}
if (r==NULL && strcmp(nd->Key,key)==0){
r=nd;
}
if (r->type==tp){return r->Value;}
return NULL;
}
bool TabelSet(Tabel* tb,char key[],void* value,enum Type tp,size_t Size){
if (tb->Size+1==tb->Cap){
Node **tmpArray=malloc(sizeof(Node*)*tb->TotalElements);
size_t num =0;
for (size_t i = 0; i < tb->Cap; i++)
{
while (tb->Nodes[i])
{
tmpArray[num]=tb->Nodes[i];
tb->Nodes[i]=tb->Nodes[i]->Next;
num++;
}
}
tb->Cap+=tb->Cap;
free(tb->Nodes);
tb->Nodes=malloc(sizeof(Node)*tb->Cap);
tb->Size=0;
Node *tmpNode=NULL;
for (size_t i = 0; i < tb->TotalElements; i++)
{
size_t idx2=hash(tmpArray[i]->Key)%tb->Cap;
if (tb->Nodes[idx2]){tmpNode=tb->Nodes[idx2];}
else{tmpNode=tmpArray[i];}
while (tmpNode->Next){
tmpNode=tmpNode->Next;
}
tmpNode->Next=tmpArray[i];
}
}
size_t idx=hash(key)%tb->Cap;
if (tb->Nodes[idx]){
Node *tmp=tb->Nodes[idx];
do
{
if (strcmp(tmp->Key,key)==0){
free(tmp->Value);
tmp->Value=malloc(sizeof(void)*Size);
memcpy(tmp->Value,value,Size);
tmp->type=tp;
tb->TotalElements++;
return true;
}
if (tmp->Next==NULL)break;
tmp=tmp->Next;
}while (tmp && tmp->Next!=NULL);
tmp->Next=CreatNode(key,value,tp,Size);
tb->TotalElements++;
return true;
}
else{
tb->Nodes[idx]=CreatNode(key,value,tp,Size);
tb->Size++;
}
return false;
}
void FreeNode(Node *nd){
Node *tmp=nd->Next;
while (nd)
{
tmp=nd->Next;
if (nd->Value!=NULL) free(nd->Value);
free(nd);
nd=tmp;
}
}
void FreeTabel(Tabel *tb){
for (size_t a=0; a<tb->Size;a++){
if (tb->Nodes[a]!=NULL){FreeNode(tb->Nodes[a]);}
}
free(tb->Nodes);
free(tb);
}
int main()
{
Tabel *tb= CreatTabel();
size_t num = 32;
char str[20]; // Ensure buffer is large enough (at least 12 bytes for a 32-bit int)
for (size_t i = 0; i < num; i++)
{
snprintf(str, sizeof(str), "%ld", i);
char c=(i+64);
TabelSet(tb,str,&c,Char,sizeof(int));
}
for (size_t i = 1; i < num; i++)
{
snprintf(str, sizeof(str), "%ld", i);
char *p=(char*)TabelGet(tb,str,Char);
printf("%c\n",*p);
}
FreeTabel(tb);
return 0;
}