【发布时间】: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 中,代码就会崩溃。我尝试了多种方法,但似乎没有任何效果。
【问题讨论】: