[英]Hash-table - Array of Linked-list - C++
I'm trying to create a Hash-table by using an array on linked-nodes (making a linked list). 我正在尝试通过在链接节点上使用数组来创建哈希表(创建链接列表)。 But I'm having difficulties inserting a value into the Hash-table.
但是我在将值插入Hash表时遇到了困难。 When I run it, I get this:
当我运行它时,我得到了这个:
http://gyazo.com/3a28a70e66b3ea34e08223e5948f49c0.png http://gyazo.com/3a28a70e66b3ea34e08223e5948f49c0.png
Here is my code: 这是我的代码:
#include <iostream>
using namespace std;
class Node {
public:
int num;
Node * next;
};
class intHashTable {
private:
int size;
Node ** table;
public:
intHashTable(int size); // construct a new hash table with size elements
~intHashTable(); // delete the memory for all internal components
void insert(int num); // insert num into the hash table, no effect
// if num is already in table
void remove(int num); // remove num from the hash table, no effect if not in table
int lookup(int num); // return 1 if num is already in table, 0 otherwise
void print(void); // print the elements of the hash table to the screen
};
// construct a new hash table with nelements elements
intHashTable::intHashTable(int nelements)
{
size = nelements;
table = new Node*[size];
for ( int i = 0; i < size; i++ ) {
table[i] = NULL;
}
}
intHashTable::~intHashTable()
{
for(int i=0; i<size; i++)
{
Node* temp = table[i];
while(temp != NULL)
{
Node* next = temp->next;
delete temp;
temp = next;
}
}
size = 0;
delete[] table;
}
void intHashTable::insert(int num){
int location = ((unsigned)num) % size;
Node *runner = table[location];
if(runner == NULL ){
runner->num = num;
}else{
while(runner != NULL ){
runner = runner->next;
}
runner->num = num;
}
}
int main(){
intHashTable a (10);
a.insert(2);
return 0;
}
After construction of intHashTable
, all the elements of table
are still NULL
. 在构造
intHashTable
之后, table
所有元素仍然是NULL
。 However, in the function insert
, one element is dereferenced: 但是,在函数
insert
,一个元素被解除引用:
Node *runner = table[location];
runner = runner->next;
This makes the program crash, because it is illegal to dereference a null pointer . 这会导致程序崩溃,因为取消引用空指针是非法的 。
the logic here is wrong 这里的逻辑是错误的
int location = ((unsigned)num) % size;
Node *runner = table[location];
if(runner == NULL ) // if null u dereference it!
{
runner->num = num;
}
else
{
while(runner != NULL ) { // u loop until null
runner = runner->next;
}
runner->num = num; // once u reach null u dereference it!
}
i would suggest instead: 我会建议:
first a ctor
for your Node 首先是你的节点的
ctor
class Node {
public:
int num;
Node * next;
Node( int _n ) : num(_n), next(NULL) { }
};
and then 然后
if ( runner != NULL )
{
while ( runner->next != NULL )
{
runner = runner->next;
}
runner->next = new Node( num );
}
else
{
table[location] = new Node( num );
}
This code certainly won't work: 这段代码肯定不会起作用:
if(runner == NULL ){
runner->num = num;
If runner is NULL, then you should never dereference it (using * or -> on it). 如果runner是NULL,那么你永远不应该取消引用它(使用*或 - >)。
Node *runner = table[location];
runner = runner->next;
if(runner == NULL )
You never verified whether table[location]
is null. 您从未验证
table[location]
是否为null。 But during construction of your hashtable, there are no nodes inside the node table (you set yourself every entry to null). 但是在构造哈希表时,节点表中没有节点(您自己将每个条目设置为null)。
The problem with your code is that you never think about allocating your node. 代码的问题在于您从不考虑分配节点。 You should be doing
你应该这样做
Node* toInsert = new Node;
toInsert->next= NULL;
toInsert->num = num;
if(table[location]==NULL){
table[location] = toInsert;
}
else{
Node *runner = table[location];
while(runner->next != NULL){
runner = runner->next;
}
runner->next = toInsert;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.