Answer to Question #278632 in C++ for Roe

Question #278632

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

1
Expert's answer
2021-12-13T00:02:38-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog