Any insight/implement to the following functions would be appreciated. Note that
ID: 3724407 • Letter: A
Question
Any insight/implement to the following functions would be appreciated. Note that the bucket number where you put a key-value pair is HashTable hash(key) mod NumberOfBuckets.
(C program)
HashTable* newHashTable(unsigned int mxs) {
// implement a constructor that builds a hashtable relying on auxiliary chaining. Write a hash function that is suitable for strings.
}
static HTNode* findInList(HTNode* head, char * key) {
//given a head of a list, return the node that has the key
}
void HashTable_add (HashTable * t, char * key, char * value) {
//add the key/value to the hash table t. DO NOTHING if the key is already in the hash table
}
char* HashTable_get(HashTable* t, char* key) {
//returns the value associated to key in t. if there is no such value return NULL
}
void HashTable_remove(HashTable *t, char* key) {
//removes the pair identified by key from the hashtable t
unsigned int HashTable_max_length(HashTable* t) {
//find the length of the longest list. return 0 if the hash table is empty
l typedef struct HTNodeTag t char char struct HTNodeTagnext; key; value HTNode; 7 typedef struct HashTableTag t unsigned int Number of buckets in the hash table HTNode* ht: 0 HashTable 2 unsigned int HashTable hash (char* str); 3 HashTable neHashTable (unsigned int mxs) 1 void 5 char * 6 void 7 void 8 char* 9 unsigned int HashTable max_length (HashTable t); HashTable_add (HhTable*t,char- key, char value) HashTable get (HashTable* t, char* key) HahTable_remove (HashTable t, char key) deleteHashTable (HashTable* t) HahTable_max_key (HashTablet char )Explanation / Answer
here is your functions : ----------->>>>>>>>>>>.
#include<stdio.h>
#include<string.h>
typedef struct HTNodeTag{
char *key;
char *value;
struct HTNodeTag *next;
}HTNode;
typedef struct HashTableTag{
unsigned int no;
HTNode **ht;
}HashTable;
unsigned int hash(char *key,int max){
unsigned int h = 0,i;
for(i = 0;i<strlen(key);i++){
h = h + (int)key[i];
}
return (int)h%max;
}
HashTable* newHashTable(unsigned int mxs) {
// implement a constructor that builds a hashtable relying on auxiliary chaining. Write a hash function that is suitable for strings.
HashTable *new_hash = (HashTable *)malloc(sizeof(HashTable));
new_hash->no = mxs;
new_hash->ht = (HTNode *)malloc(sizeof(HTNode*) * mxs);
}
static HTNode* findInList(HTNode* head, char *key) {
//given a head of a list, return the node that has the key
if(head == NULL){
return NULL;
}
HTNode *temp = head;
while(temp != NULL){
if(strcmp(key,temp->key) == 0){
return temp;
}
temp = temp->next;
}
return NULL;
}
void HashTable_add (HashTable *t, char *key, char *value) {
//add the key/value to the hash table t. DO NOTHING if the key is already in the hash table
unsigned int n = hash(key,t->no);
HTNode *temp = findInList(t->ht[n],key);
if(temp == NULL){
HTNode *tmp = (HTNode *)malloc(sizeof(HTNode));
tmp->key = (char *)malloc(sizeof(char)*strlen(key));
strcmp(tmp->key,key);
tmp->value = (char *)malloc(sizeof(char)*strlen(value));
strcmp(tmp->value,value);
tmp->next = NULL;
if(t->ht[n] == NULL){
t->ht[n] = tmp;
return;
}
temp = t->ht[n];
while(temp->next != NULL){
temp = temp->next;
}
temp->next = tmp;
}
}
char* HashTable_get(HashTable* t, char* key) {
//returns the value associated to key in t. if there is no such value return NULL
unsigned int n = hash(key,t->no);
HTNode *temp = findInList(t->ht[n],key);
if(temp == NULL){
return "";
}
return temp->value;
}
void HashTable_remove(HashTable *t, char* key) {
unsigned int n = hash(key,t->no);
HTNode *temp = t->ht[n];
HTNode *prev = NULL;
while(temp != NULL){
if(strcmp(temp->key,key) == 0){
if(prev == NULL){
t->ht[n] = temp->next;
free(temp);
return;
}
prev->next = temp->next;
free(temp);
return;
}
temp = temp->next;
}
}
//removes the pair identified by key from the hashtable t
unsigned int HashTable_max_length(HashTable* t){
int i;
int max = 0;
int temp = 0;
for(i = 0;i<t->no;i++){
temp = 0;
HTNode *tmp = t->ht[i];
while(tmp != NULL){
temp++;
tmp = tmp->next;
}
if(temp > max){
max = temp;
}
}
}
int main(){
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.