rename tree lectures
This commit is contained in:
26
lectures/23_hash_tables_I/hash_phonebook_code.cpp
Normal file
26
lectures/23_hash_tables_I/hash_phonebook_code.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user