使用HashTable的读取访问冲突

时间:2019-04-21 04:36:41

标签: c++ exception hashtable

我目前正在研究一个程序,该程序可以从学生数据库创建哈希表。在尝试运行程序时,我遇到了一个异常,该异常表示读取访问冲突。我不确定为什么会这样。

我尝试在代码的某些区域添加模数,因为我认为问题在于我返回的值对于哈希表而言太大。遗憾的是这没有用,谢谢您的帮助

以下是导致抛出异常的函数:

是空函数:

bool isEmpty() const {
    return(type == emptySlot);
}

插入功能:

bool HashTable::insert(int key, int value, int& collisions)
{
    int hashedArrayIndex = ::hash(key);
    int homeArrayIndex = hashedArrayIndex % MAXHASH;
    int takenIndex = 0; 
    int tmepIndex = 0;
    while (!takenIndex)
    {
        if (array[hashedArrayIndex].isEmpty() || 
            array[hashedArrayIndex].isTombstone())
        {
            array[hashedArrayIndex] = Slot(key, value);
            count++;
            return true;
            return collisions;
        } 
        else if (array[hashedArrayIndex].isNormal())
        {
            if (array[hashedArrayIndex].getKey() == key)
            {
                return false;
            } 
            else
            {
                collisions++;
                hashedArrayIndex = (homeArrayIndex + probingArray[tmepIndex]) % MAXHASH;
                tmepIndex++;
            }
        }
    }
    return false;
}

我的哈希表构造函数:

HashTable::HashTable()
{
    float count = 0;

    Slot array[arraySize];
    const int arraySize = MAXHASH;
    int probingArray[arraySize];

    for (int i = 0; i < arraySize; i++)
    {
        probingArray[i] = i;
    }
}

0 个答案:

没有答案
相关问题