-1

对于初学者,我的程序是创建一个名称哈希表,其中通过链接处理 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;
    }
}
   }
4

1 回答 1

2

正如您正确怀疑的那样,您的问题是您的堆栈空间不足。您不应该将这么大的对象放在堆栈上,这是一种有限且宝贵的资源。相反,您可以使用指针并在堆上动态分配它们。

有问题的代码在这里:

int main() {
char command;
int listSize = 88801;
LinkedList HashList1[88801]; // << this
LinkedList HashList2[88801]; // << and this

另请注意,这LinkedList* hashbucket1[88801]是一个指针数组,而不是指向数组的指针。你想要的是:

LinkedList (*HashList1)[88801]

或者,typedef为了使其更具可读性:

typedef LinkedList LinkedListArray[88801];
LinkedListArray* HashList1;

但到那时最好这样做:

LinkedList *HashList1 = new LinkedList[88801];
于 2012-05-27T00:02:17.540 回答