C++ 哈希表加法器分离链分段错误
我的单独链接哈希表对象中出现分段错误C++ 哈希表加法器分离链分段错误,c++,data-structures,hashtable,chaining,C++,Data Structures,Hashtable,Chaining,我的单独链接哈希表对象中出现分段错误 bool hashTable::addNode(int id, string information){ bool inserted = false; int position = hash(id); cout << &hashtable[position]<<endl; if(hashtable[position]==NULL){ hashtable[position]->data.id = id;
bool hashTable::addNode(int id, string information){
bool inserted = false;
int position = hash(id);
cout << &hashtable[position]<<endl;
if(hashtable[position]==NULL){
hashtable[position]->data.id = id;
hashtable[position]->data.information = information;
hashtable[position]->next = NULL;
inserted = true;
} else {
Node* current = new Node;
current = hashtable[position];
while(current!=NULL){
if(id < hashtable[position]->data.id){
current->data.id = id;
current->data.information = information;
current->next = hashtable[position];
hashtable[position] = current;
inserted = true;
} else if(id < current->data.id){
current->data.id = id;
current->data.information = information;
current->next = hashtable[position]->next;
hashtable[position]->next = current;
inserted = true;
} else if(current->next==NULL){
Node *temp;
temp->next = NULL;
temp->data.id = id;
temp->data.information = information;
current->next = temp;
inserted = true;
}
current = current->next;
}
}
return inserted;
bool哈希表::addNode(int-id,字符串信息){
bool-inserted=false;
int位置=散列(id);
cout-next=NULL;
插入=真;
}否则{
节点*当前=新节点;
当前=哈希表[位置];
while(当前!=NULL){
if(iddata.id){
当前->数据.id=id;
当前->数据信息=信息;
当前->下一步=哈希表[位置];
哈希表[位置]=当前;
插入=真;
}else if(iddata.id){
当前->数据.id=id;
当前->数据信息=信息;
当前->下一步=哈希表[位置]->下一步;
哈希表[position]->next=当前;
插入=真;
}else if(当前->下一步==NULL){
节点*温度;
temp->next=NULL;
temp->data.id=id;
temp->data.information=信息;
当前->下一步=温度;
插入=真;
}
当前=当前->下一步;
}
}
返回插入;
}
本质上,我有一个头指针数组来处理单独的链接,但是addNode中的分段错误把我搞砸了。为了清楚起见,我首先调用一个公共AddEntry,它为数组中的每个LinkedList调用AddNode
#ifndef HASH_H
#define HASH_H
#include <iostream>
#include <string>
#include "data.h"
using std::string;
using std::cout;
using std::endl;
#define SIZE 15
class hashTable{
public:
hashTable();
~hashTable();
bool addEntry(int id, string information);
string getEntry(int id);
bool removeEntry(int id);
int getCount();
void displayTable();
private:
bool removeNode(int id, int position);
bool addNode(int id, string information);
int count = 0;
Node* hashtable = new Node[SIZE]; //array of head pointers
int hash(int id);
};
#endif
\ifndef散列
#定义哈希值
#包括
#包括
#包括“data.h”
使用std::string;
使用std::cout;
使用std::endl;
#定义尺寸15
类哈希表{
公众:
哈希表();
~hashTable();
布尔加法器(int-id,字符串信息);
字符串getEntry(int id);
bool removeEntry(内部id);
int getCount();
void displayTable();
私人:
布尔removeNode(内部id,内部位置);
bool addNode(int-id,字符串信息);
整数计数=0;
Node*hashtable=新节点[SIZE];//头指针数组
int散列(int-id);
};
#恩迪夫
很抱歉,如果我没有说得最好,这是我第一次提到堆栈溢出。您的代码中有几个地方看起来您使用的运算符地址不正确。让我们从这个开始:
if (&hashtable[position] == NULL) {
...
}
我完全明白您在这里试图做什么-您试图说“如果索引位置的插槽持有空指针。”但是,这段代码实际上并不是这样做的。这表示“如果内存中存在hashtable[position]
的位置本身就是一个空指针。”这是不可能的-hashtable[position]
引用数组中的一个插槽-因此此if
语句从不触发
在这里画图可能有助于更好地了解正在发生的事情。例如,假设您有一个空哈希表,如下所示:
+-------+ +------+------+------+------+ +------+
| |------>| null | null | null | null | ... | null |
+-------+ +------+------+------+------+ +------+
hashtable
这里,hashtable[position]
指的是hashtable
指向的数组中索引position
处的指针。对于空哈希表,hashtable[position]
将计算为NULL
,因为这是该插槽指针的内容。另一方面,&hashtable[position]
指的是这样的东西:
&hashtable[position]
+-------+
| |
+-------+
|
v
+-------+ +------+------+------+------+ +------+
| |------>| null | null | null | null | ... | null |
+-------+ +------+------+------+------+ +------+
hashtable
这里,&hashtable[position]
指向数组中的一个指针。虽然指针&hashtable[position]
指向的是空指针,但它本身不是空指针,因为它指向内存中的有效对象
将代码更改为只读
if (hashtable[position] == NULL) {
...
}
正确表达了“如果哈希表
数组中位于索引位置
的项不是空指针。”
代码中还有一些类似的错误。请仔细阅读您的资料,并注意以下问题:我是希望指针存储在数组中的某个索引处,还是希望该指针恰好位于内存中的某个位置?请说出您使用的语言using@asker我正在使用C++哇,非常感谢!我完全明白我错在哪里了。然而,我仍然在同一点上有一个分割错误。我编辑这篇文章是为了让你知道我改变了什么。再次感谢!问题可能出在hash.h文件(第二段代码)@templatetypedef再次感谢@templatetypedef中,你知道是什么原因造成的吗?