this hash function uses linked list to resolve collsions.
typedef struct {
char num[4];
int id;
} CUSTOMER // Data
typedef struct Node* nd;
typedef struct Node{
CUSTOMER cust;
nd next;
} NODE;
typedef struct{
nd head;
} HASH;
The customer struct is the data of the the node pointer.
The NODE pointer cointains the CUSTOMER data and a pointer to the next NODE.
The HASH struct is what is being used as the array type.
ex. HASH hash[TableSize]
;
intializes all hash pointers to NULL.
void Hash_Initialize(HASH hash[]){
for(int i = 0; i < TableSize; i++){
hash[i] = NULL;
}
}
adds a new Item in the hash table.
void Hash_Add(HASH hash[], CUSTOMER cust){
int index;
index = cust.id % TableSize;
nd tp = malloc(sizeof(NODE));
tp->cust = cust;
tp->next = NULL:
if(hash[index].head != NULL)
tp->next = hash[index].head;
hash[index].head = tp;
}
deletes the item you want in the hash table.
bool Hash_Delete(HASH hash[], int key){
int index;
bool found = false;
index = key % TableSize;
nd tp = hash[index].head, tp1;
while(tp != NULL && !found){
if(tp->cust.id == key){
if(tp1 == NULL)
hash[index].head = NULL;
else if(tp->next == NULL)
tp1->next = NULL;
else
tp1->next = tp->next;
else
found = true;
free(tp);
}
else{
tp1 = tp;
tp = tp->next;
}
}
return found;
}
Finds the item you want in the hash table and returns it as a parameter.
bool Hash_Search(HASH hash[], int key, CUSTOMER *data){
int index;
bool found = false;
index = cust.id % TableSize;
nd tp = hash[index].head, tp1;
while(tp != NULL && !found){
if(tp->cust.id == key){
*data = tp->cust;
}
else{
tp1 = tp;
tp = tp->next;
}
}
return found;
}