My rehash function doesn't run when compiling code

Viewed 37

When I run my code (below), the first HashMap prints, however the second one doesn't. The second HashMap is just rehashing the same numbers but using a smaller table size (7 instead of 17 like the first HashMap). Can someone help me understand why my rehashed values won't print ?

NOTE: in my code, "insert" = hash & reinsert = "reinsert"

#include<iostream>
using namespace std;

// class HashEntry to store key value pair
class HashEntry {
private:
    int key; // key of value
    int data; // data to store
public:
    // constructor with two parameters
    HashEntry(int key, int data) {
        this->key = key; // set key
        this->data = data; // set data
    }

    // getter for key
    int getKey() const {
        return key; // return key
    }

    // getter for value
    int getValue() const {
        return data; // return data
    }
};

int TABLE_SIZE = 17;
class HashMap {
private:
    HashEntry **table;
public:
    //default constructor
    HashMap() {
        table = new HashEntry*[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++)
            table[i] = NULL;
    }

    // get value for given key
    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();
    }

    // insert value to hashtable
    void insert(int value) {
        int sum = 0;
        int temp = value;
        while (temp != 0) {
            int mod = temp % 10;
            sum += mod;
            temp = temp / 10;
        }
        int hash = (sum % TABLE_SIZE);
        while (table[hash] != NULL)
            hash = (hash + 1) % TABLE_SIZE;
        if (table[hash] == NULL)
            table[hash] = new HashEntry(hash, value);
    }
    
    void reinsert(int value) {
        TABLE_SIZE = 7;
        int sum = 0;
        int temp = value;
        while (temp != 0) {
            int mod = temp % 10;
            sum += mod;
            temp = temp / 10;
        }
        int hash = (sum % TABLE_SIZE);
        while (table[hash] != NULL)
        hash = (hash + 1) % TABLE_SIZE;
        if (table[hash] == NULL)
            table[hash] = new HashEntry(hash, value);
    }

    ~HashMap() {
        for (int i = 0; i < TABLE_SIZE; i++)
            if (table[i] != NULL)
                delete table[i];
        delete[] table;
    }
};


int main(){
    HashMap hm;
    // insert values to hashmap
    hm.insert(121);
    hm.insert(81);
    hm.insert(16);
    hm.insert(100);
    hm.insert(25);
    hm.insert(0);
    hm.insert(1);
    hm.insert(9);
    hm.insert(4);
    hm.insert(36);
    hm.insert(64);
    hm.insert(49);

    //Print inserted numbers
    for (int i=0; i<17; i++) {
        cout<<hm.get(i)<<", ";
    }
    
    hm.reinsert(121);
    hm.reinsert(81);
    hm.reinsert(16);
    hm.reinsert(100);
    hm.reinsert(25);
    hm.reinsert(0);
    hm.reinsert(1);
    hm.reinsert(9);
    hm.reinsert(4);
    hm.reinsert(36);
    hm.reinsert(64);
    hm.reinsert(49);

    //Print reinserted numbers
    for (int i=0; i<7; i++) {
        cout<<hm.get(i)<<", ";
    }
}
0 Answers
Related