person Walt={.name="Walt",.age=26}; person Skyler={.name="Skyler",.age=27}; person Saul={.name="Saul",.age=28}; person Mike={.name="Mike",.age=29}; person Hank={.name="Hank",.age=30}; person Mary={.name="Mary",.age=31};
//add person into hash_table hash_table_insert(&Walt); hash_table_insert(&Skyler); hash_table_insert(&Saul); hash_table_insert(&Mike); hash_table_insert(&Hank); hash_table_insert(&Mary);
print_table();
//delete persion in hash table hash_table_delete(&Walt); print_table();
return0; }
//define the hash function unsignedinthash(char *name){ int length=strnlen(name, MAX_NAME); unsignedint hash_value=0; for(int i=0;i<length;i++){ hash_value+=name[i]; hash_value=(hash_value*name[i])%TABLE_SIZE; } return hash_value; }
//initialize the hash table to NULL (let it be empty) voidinit_hash_table(){ for(int i=0;i<TABLE_SIZE;i++){ hash_table[i]=NULL; } }
//print out the hash table voidprint_table(){ printf("--------Start--------\n"); for(int i=0;i<TABLE_SIZE;i++){ //nothing in the hash table if(hash_table[i]==NULL) printf("\t%d\t---\n",i); //something in the hash table elseprintf("\t%d\t%s\n",i,hash_table[i]->name); } printf("--------End----------\n"); }
//insert the element into the hash_table boolhash_table_insert(person *p){ //make sure not call this function with null ptr if(p==NULL) returnfalse;
//insert the person into the index return by hash_function int index=hash(p->name); //check the pointer at that index in the table is null or not if(hash_table[index]!=NULL) returnfalse; //collision, somebody had occupied there already //if no one occupied there, then occupied the address hash_table[index]=p; returntrue; }
boolhash_table_insert(const Person& p){ auto result = hash_table.insert({p.name, p}); return result.second; // True if insertion took place, false if key already existed }
boolhash_table_delete(const string& name){ return hash_table.erase(name) > 0; // True if element was removed, false if key didn't exist }
// Delete person from hash table hash_table_delete("Walt"); print_table();
return0; }
// Define the hash function unsignedinthash(char *name) { int length = strnlen(name, MAX_NAME); unsignedint hash_value = 0; for (int i = 0; i < length; i++) { hash_value += name[i]; hash_value = (hash_value * name[i]) % TABLE_SIZE; } return hash_value; }
// Initialize the hash table to NULL (let it be empty) voidinit_hash_table() { for (int i = 0; i < TABLE_SIZE; i++) { hash_table[i] = NULL; } }
// Print out the hash table voidprint_table() { printf("--------Start--------\n"); for (int i = 0; i < TABLE_SIZE; i++) { if (hash_table[i] == NULL) { printf("\t%d\t---\n", i); } else { person *tmp = hash_table[i]; while (tmp != NULL) { printf("\t%d\t%s\n", i, tmp->name); tmp = tmp->next; } } } printf("--------End----------\n"); }
// Insert the element into the hash_table boolhash_table_insert(person *p) { if (p == NULL) returnfalse;
int index = hash(p->name);
// Insert at the head of the list p->next = hash_table[index]; hash_table[index] = p;
returntrue; }
// Lookup a person in the hash table by name person* hash_table_lookup(char *name) { int index = hash(name); person *tmp = hash_table[index]; while (tmp != NULL && strncmp(tmp->name, name, MAX_NAME) != 0) { tmp = tmp->next; } return tmp; }
// Delete a person from the hash table by name boolhash_table_delete(char *name) { int index = hash(name); person *tmp = hash_table[index]; person *prev = NULL;