Using hashing
Make a hash table that searches names of a people form the staring alphabet (character) like the real life dictionary
Note:
No global declarations
Run test the code in main
#include <iostream>
using namespace std;
#define size 1024
string table[1024];
size_t hash(string str) {
size_t result = 0;
for (int i = 0; i < str.size(); i++) {
result += 31 * (int) str[i];
}
return result % size;
}
void put(string str, string value) {
table[hash(str)] = value;
}
string get(string str) {
return table[hash(str)];
}
int main() {
string name = "john";
string value = "apple";
push(name, value);
cout << get(name) << endl; // apple
return 0;
}
Comments
Leave a comment