【问题标题】:Insert Function in a Hash Table C++在哈希表 C++ 中插入函数
【发布时间】:2020-02-26 07:51:37
【问题描述】:

所以我正在处理与以前相同的哈希表。我一直在尝试实现插入功能,但它的行为很奇怪。获取此信息并将其发布到 DATAIN 的文件中:

TATUNG CO.EL PR. LONG BEACH CA
KAMERMAN LCIRRUS BEAVERTON, OR
QUADRAM COLOACH AV NORCROSS GE
AST RESEARALTON AV IRVINE   CA
EXPRESS SYREMING SCHAUMBURG IL
DAC SW INCSPRING VAL DALLAS TX

这是分成单个存根的代码。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <string.h> // for strcpy()

using namespace std;

typedef char STR10[10+1];
typedef char STR20[20+1];

struct SLOT
{
  STR10 key;
  STR20 data;
};

struct BUCKET
{
  SLOT entry[3];
  int count;
  int overflow;
};

struct HASHTABLE
{
  BUCKET pBkt[20];
  BUCKET oBkt[10];
  int nextFreeOBucket = 0;
};

void InitializeHT (HASHTABLE ht, int MAXPBUCKETS, int MAXOBUCKETS, int MAXSLOTS);
int HashFunction (STR10 key);
void InsertIntoHT (HASHTABLE ht, STR10 &key, STR20 &data);

int main()
{
    int maxP = 20;
    int maxO = 10;
    int maxS = 3;
    HASHTABLE ht;

    STR10 mKey;
    STR20 mData;
    STR10 sKey;

    InitializeHT (ht, maxP, maxO, maxS);

    FILE * inFile;
    inFile = fopen("DATAIN.dat","rb");
    if (inFile == NULL)
    {
        cout << " DATAIN file access error ... \n";
        cout << " Terminating application ... \n ";
        cout << " Press any key ... \n ";
        return -100;
    }
    char crLF;

    while (!feof(inFile))
    {
        fscanf(inFile,"%10c%20c\n",mKey,mData);
        mKey[10] = mData[20] = 0; // add string terminators
        printf(" MyKey: %10s\n MyData: %20s\n",mKey,mData);
        cin.ignore(80,'\n'), cin.get();
        InsertIntoHT (ht, mKey, mData);
    }

    fclose(inFile);

    return 0;
}

void InitializeHT (HASHTABLE ht, int MAXPBUCKETS, int MAXOBUCKETS, int MAXSLOTS)
{
    for (int i = 0; i < MAXPBUCKETS; i++)
    {
        ht.pBkt[i].count = 0;              // set count field to 0
        ht.pBkt[i].overflow = -1;          // set overflow field to -1
    }

    for (int i = 0; i < MAXOBUCKETS; i++)
    {
        ht.oBkt[i].count = 0;              // set count field to 0
        ht.oBkt[i].overflow = -1;          // set overflow field to -1
    }
    // each slot is empty, count and overflow have been initialized
}

int HashFunction (STR10 key)
{
    int iValue = int(key[1]) + int(key[3]) + int(key[5]);
    int hashIndex = iValue % 20;

    return hashIndex;
}

void InsertIntoHT(HASHTABLE ht, STR10 key, STR20 data)
{
    int hashIndex = HashFunction(key);
    int c = ht.pBkt[hashIndex].count;
    int c2 = ht.oBkt[hashIndex].count;
    int c3 = ht.oBkt[ht.nextFreeOBucket].count;

    if (c <= 2)
    {
      strcpy(ht.pBkt[hashIndex].entry[c].key,key);
      strcpy(ht.pBkt[hashIndex].entry[c].data,data);
      cout << ht.pBkt[hashIndex].entry[c].key << " / " << ht.pBkt[hashIndex].entry[c].data << endl;
    }

    else
    {
      if (ht.pBkt[hashIndex].overflow != -1)      // it has an overflow; check the overflow
      {
        hashIndex = ht.pBkt[hashIndex].overflow;  //using the overflow field to navigate through the buckets as hashIndex

        /*strcpy(ht.oBkt[hashIndex].entry[c2].key,key); // this causes the crash
        strcpy(ht.oBkt[hashIndex].entry[c2].data,data);
        c2++;

        cout << ht.oBkt[hashIndex].entry[c2].key << " / " << ht.oBkt[hashIndex].entry[c2].data << endl;*/
      }

      else       // it does not have an overflow
      {
        ht.pBkt[hashIndex].overflow = ht.nextFreeOBucket;
        int c3 = ht.oBkt[ht.nextFreeOBucket].count;

        strcpy(ht.oBkt[ht.nextFreeOBucket].entry[c3].key,key);
        strcpy(ht.oBkt[ht.nextFreeOBucket].entry[c3].data,data);
        c3++;                       // increment the overflow bucket's count
        ht.nextFreeOBucket++;       // allocate the next free overflow bucket
        cout << ht.oBkt[ht.nextFreeOBucket].entry[c3].key << " / " << ht.oBkt[ht.nextFreeOBucket].entry[c3].data << endl;
      }
    } // else/
}

插入功能适用于某些行。但是,如果我将任何内容插入到 else if 语句中,然后将溢出分配到 hashIndex 中,代码就会崩溃。我尝试了多种方法,但似乎没有任何效果。

【问题讨论】:

    标签: c++ hashtable


    【解决方案1】:
    strcpy(ht.oBkt[hashIndex].entry[c2].key,key); // this causes the crash
    

    当然会导致崩溃,因为 oBkt 数组中只有 10 个元素,但使用 hashIndex 访问它,其值可以从 0 到 19。

    顺便说一句,为什么要使用 C 构造:字符串、文件访问等。如果您尝试用 C++ 编写?

    如果您使用 std::array 而不是本机数组,您甚至可能会收到有关崩溃发生原因的有意义的异常消息...

    【讨论】:

    • 我使用了 C 结构,因为在插入语句中使用标准 C++ 语法变得越来越复杂。顺便说一句,如果我考虑到溢出桶数组重新计算哈希函数会怎样?
    猜你喜欢
    • 2023-03-04
    • 2015-04-11
    • 2011-01-11
    • 2015-06-25
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    相关资源
    最近更新 更多