5

リンクノードの配列を使用して(リンクリストを作成して)ハッシュテーブルを作成しようとしています。しかし、ハッシュテーブルに値を挿入するのに問題があります。私がそれを実行すると、私はこれを取得します:

http://gyazo.com/3a28a70e66b3ea34e08223e5948f49c0.png

これが私のコードです:

#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;
}
4

4 に答える 4

3

の構築後intHashTable、のすべての要素tableはまだNULLです。ただし、関数insertでは、1つの要素が逆参照されます。

Node *runner = table[location];
runner = runner->next;

nullポインタを逆参照することは違法であるため、これによりプログラムがクラッシュします。

于 2013-01-22T11:56:55.157 に答える
2

ここの論理は間違っています

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!
}

私は代わりに提案します:

最初ctorにあなたのノードのために

class Node {
public:
  int num;
  Node * next;

  Node( int _n ) : num(_n), next(NULL) { } 
};

その後

if ( runner != NULL )
{
   while ( runner->next != NULL )
   {
      runner = runner->next;
   }
   runner->next = new Node( num );
}
else
{
  table[location] = new Node( num ); 
}
于 2013-01-22T12:14:17.340 に答える
1

このコードは確かに機能しません:

if(runner == NULL ){
runner->num = num;

ランナーがNULLの場合、(*または->を使用して)間接参照しないでください。

于 2013-01-22T11:58:41.823 に答える
1
Node *runner = table[location];
runner = runner->next;
if(runner == NULL )

table[location]nullかどうかを確認したことはありません。ただし、ハッシュテーブルの作成中は、ノードテーブル内にノードはありません(すべてのエントリをnullに設定します)。

コードの問題は、ノードの割り当てについて考えないことです。あなたがしている必要があります

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;
}
于 2013-01-22T12:07:29.353 に答える