rename tree lectures

This commit is contained in:
Jidong Xiao
2024-03-18 00:44:53 -04:00
parent 72b457e496
commit dc561a1eb0
16 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
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;
}