对于初学者,我的程序是创建一个名称哈希表,其中通过链接处理 collison。所以我创建了一个链表数组。问题是我的数组非常大,确切地说是 88801。我想这就是为什么我让堆栈溢出但我不确定。我的代码如下。有人试图告诉我让数组成为一组指向链表的指针。
IE 将 LinkedList* hashbucket1[88801] 这消除了堆栈溢出问题但导致其他链接错误。猜猜我试图找出实现字符串链表数组的最佳方法是什么。如果我在正确的轨道上并且它的一些小东西在看,请指出它或告诉我放弃它并从给定的路径重新开始。如果我需要链接或压缩我的其他文件,请告诉我,我可以。
#include <iostream>
#include "LinkedList.h"
#include <string>
#include <fstream>
using namespace std;
unsigned long djb2(string& str)
{
unsigned long hash = 5381;
for(string::iterator it = str.begin();it!=str.end();it++)
hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
return hash;
}
static unsigned long sdbm(string& str)
{
unsigned long hash = 0;
int c;
for(string::iterator it = str.begin();it!=str.end();it++)
hash = *it + (hash << 6) + (hash << 16) - hash;
return hash;
}
void insert(LinkedList HashList1[], LinkedList HashList2[], int listSize);
int main() {
char command;
int listSize = 88801;
LinkedList HashList1[88801];
LinkedList HashList2[88801];
bool invalidcommand;
do{
do{
cout << "MENU" << endl;
cout << "(I)nsert new entry" << endl;
cout << "(S)earch" << endl;
cout << "(D)elete Entry" << endl;
cout << "(C)reate Hast Table" << endl;
cout << "(L)og File" << endl;
cout << "(Q)uit and Exit Program" << endl;
cin >> command;
if(command == 'I'){
insert(HashList1, HashList2, listSize);
}else if(command == 'S'){
// search(HashList1, HashList2, listSize);
}else if(command == 'D'){
}else if(command == 'L'){
// save(HashList1, HashList2, listSize);
}else if(command == 'C'){
// load(HashList1, HashList2, listSize);
}
}while(command != 'Q');
return 0;
};
void insert(LinkedList HashList1[], LinkedList HashList2[], int listSize){
string lastName;
bool invalidID;
//Person newPerson;
do
{
invalidID = false;
cout << "Please enter Last Name: ";
cin >> lastName;
while(cin.fail())
{
cin.clear();
cin.ignore();
}
}while(invalidID);
//newPerson.setLastName(lastName);
int hashBucket1;
int hashBucket2;
hashBucket1 = djb2(lastName) % listSize;
hashBucket2 = sdbm(lastName) % listSize;
LinkedList bucket1;
if(!HashList1[hashBucket1].find(lastName)){
//HashList1[hashBucket1].insert(newPerson);
HashList1[hashBucket1].insert(lastName);
}else{
if(!bucket1.find(lastName)){
HashList1[hashBucket1].insert(lastName);
}else{
cout << "List already contains an entry with the last name " << lastName << endl;
}
}
LinkedList bucket2;
if(HashList2[hashBucket2].find(lastName)){
// bucket2.insert(newPerson);
}else{
if(!bucket2.find(lastName)){
//bucket2.insert(newPerson);
}else{
cout << "List already contains an entry with the last name " << lastName << endl;
}
}
}