#include using namespace std; #define LEN 11 // We're going to have to store something more like this: struct TableEntry { string key; int value; }; struct TableEntry table[LEN]; int make_hash_value(string s){ int sum = 0; for(auto c : s) sum += c; return sum % LEN; } bool insert(string key, int value){ int location = make_hash_value(key); while(table[location].key != "") if(table[location].key == key){ return false; location++; } table[location].key = key; table[location].value = value; return true; } int& retrieve(string key){ int location = make_hash_value(key); while(table[location].key != ""){ if(table[location].key == key) return table[location].value; location++; location %= LEN; } return location; } /* template struct TableEntry{ Ktype key; Vtype value; };*/ int main(){ cout << "Buffalo " << make_hash_value("Buffalo") << endl; cout << "Bean " << make_hash_value("Bean") << endl; cout << "Fruitcake " << make_hash_value("Fruitcake") << endl; insert("Buffalo", 3); insert("Bean", 90); insert("Fruitcake", 1); insert("Muffalo", 4); retrieve("Buffalo") += 10; cout << "We have " << retrieve("Buffalo") << " buffalo\n"; cout << "We have " << retrieve("Fruitcake") << " fruitcake\n"; cout << "We have " << retrieve("Muffalo") << " muffalo\n"; return 0; }