Code for hash.cpp:
const int TABLE_SIZE = 128; class HashMap { private: HashEntry **table; public: HashMap() { table = new HashEntry*[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) table[i] = NULL; } int get(int key) { int hash = (key % TABLE_SIZE); while (table[hash] != NULL && table[hash]->getKey() != key) hash = (hash + 1) % TABLE_SIZE; if (table[hash] == NULL) return -1; else return table[hash]->getValue(); } void insert(int value) { /* your code here*/ } ~HashMap() { for (int i = 0; i < TABLE_SIZE; i++) if (table[i] != NULL) delete table[i]; delete[] table; } };
Not entirely sure what HashEntry is, just assumed it was the name of a class
First of all in hash table we can store store data in the form of key and value pair.
You can replace the insert function by below code-
void insert(int key, int value)
{
int hash=key%TABLE_SIZE;
while(table[hash]!=NULL && table[hash]->getKey()!=key)
{
hash=(hash+1)%TABLE_SIZE;
}
if(table[hash]!=NULL)
delete table[hash];
table[hash]=new HashEntry(key,value);
}
To insert the array of integer you can treatthe sequence as key and array element as data.
Get Answers For Free
Most questions answered within 1 hours.