Files
CSCI-1200/lectures/20_hash_tables_I/hash_phonebook_code.cpp
2023-11-05 21:49:17 -05:00

27 lines
814 B
C++

int hash_function(int number) {
//return 5; /// BAD: always the same
//return number / 1000000000; /// BAD: first bad
return number % PHONEBOOK_SIZE; // REASONABLY GOOD:
}
void add(Node* phonebook[PHONEBOOK_SIZE], int number, const std::string& name) {
int index = hash_function(number) % PHONEBOOK_SIZE;
Node* tmp = new Node;
tmp->name = name;
tmp->number = number;
tmp->next = phonebook[index];
phonebook[index] = tmp;
// what about duplicate / repeated add?
}
std::string identify(Node* phonebook[PHONEBOOK_SIZE], int number) {
Node* current = phonebook[ hash_function(number) % PHONEBOOK_SIZE ];
while ( current != NULL && current->number != number ) {
current = current->next;
}
if (current == NULL) return "UNKNOWN CALLER";
return current->name;
}