C++ linkedlist数组的堆栈溢出问题
对于初学者,我的程序是创建一个名称哈希表,其中collison由链接处理。 所以我创建了一个链表数组。问题是我的阵列非常大,准确地说是88801。我在想,这就是为什么我会有一大堆问题,但我不确定。我的代码如下。有人试图告诉我将数组设置为指向链表的一组指针。C++ linkedlist数组的堆栈溢出问题,c++,C++,对于初学者,我的程序是创建一个名称哈希表,其中collison由链接处理。 所以我创建了一个链表数组。问题是我的阵列非常大,准确地说是88801。我在想,这就是为什么我会有一大堆问题,但我不确定。我的代码如下。有人试图告诉我将数组设置为指向链表的一组指针。 IE放置LinkedList*hashbucket1[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;
}
}
}
#包括
#包括“LinkedList.h”
#包括
#包括
使用名称空间std;
无符号长djb2(字符串和str)
{
无符号长散列=5381;
for(string::iterator it=str.begin();it!=str.end();it++)
hash=((hash正如您正确怀疑的那样,您的问题是堆栈空间不足。您不应该在堆栈上放置如此大的对象,因为这是一种有限而宝贵的资源。相反,您可以使用指针并在堆上动态分配它们
问题代码如下:
int main() {
char command;
int listSize = 88801;
LinkedList HashList1[88801]; // << this
LinkedList HashList2[88801]; // << and this
或者,使用typedef
使其更具可读性:
typedef LinkedList LinkedListArray[88801];
LinkedListArray* HashList1;
但到那时,最好是:
LinkedList *HashList1 = new LinkedList[88801];
谢谢你,我想这就是原因。作为一个参数,它看起来如何?我试着用void insert(LinkedList(*HashList1[])、LinkedList(*HashList2[])、int listSize替换我的insert函数,但它说参数和参数不匹配。为了确保我理解正确的LinkedList(*HashList1)[88801];是指向数组的指针,但它也是指针数组吗?因为当我向该数组中插入某个内容时,它需要是一个链表,以处理哈希中collison的链接function@Jeremy:如果您使用最新的替代方案,并像使用常规指针一样使用它,则会更容易使用LinkedList(*HashList1)[88801]
声明您的类型,然后作为函数参数,您可以编写LinkedList(*HashList1)[88801]
或LinkedList(*HashList1)[
,因为数组衰减,实际上这里的意思相同。@Jeremy:LinkedList(*HashList1)[88801
是指向LinkedList
数组的指针,而不是指向LinkedList
数组的指针。数组中的每个元素都属于LinkedList
类型,应该适合您,对吧?请接受您已经提出的问题的一些答案;有关详细信息,请参阅。
LinkedList *HashList1 = new LinkedList[88801];