在哈希表单独链接中没有用于调用xxx的匹配函数

时间:2015-11-24 08:28:42

标签: c++ algorithm hashtable chaining

目前我正在尝试使用Separate Chaining算法将二进制搜索树转换为哈希表,以便在DEV-C ++中开发一个简单的程序。

到目前为止,我已经转换了插入,删除,搜索和显示操作函数以及哈希函数。

item list.txt:

    Item ID  Item Price Item Name
    10001     23.00     Book1 
    10002     24.00     Book2
    10003     31.98     Book3
    10004     41.90     Book4

我遇到的部分是我不知道如何将 item list.txt 的所有项目插入哈希表。

散列文本文件数据的功能:

void fillHashTable(HashTableSeparateChaining hashtable){
ifstream file;
file.open("item list.txt");
if(!file) {
    cout<<" Error opening file. " << endl;}

int item_id;
double price;
string item_name;

Item i;
struct node* A[M];

while(file >> item_id >> price >> item_name)
{
    i.setItem_id(item_id);
    i.setPrice(price);
    i.setItem_name(item_name);

    HashTableSeparateChaining hashtable;
    int hv = hash_function( i.getItem_id());       

    hashtable.insert(&A[hv], &A[hv], i);
}
file.close();}

类HashTableSeparateChaining:

class HashTableSeparateChaining{
  private:
          struct node
          {
                 struct node *next;//SINGLY-LINKED LIST
                 struct node *prev;
                 Item data;
          };

  public:

        void insert(struct node **h, struct node **t, Item i);
        void Delete(struct node **h, struct node **t, int i);
        void display( struct node* h );
        void search( struct node *h, int key );                          
};

插入方法:

void HashTableSeparateChaining::insert(struct node **h, struct node **t, Item i){
 struct node *n = new node;

if( *h == NULL ) {
    n->data = i;
    n->next = NULL;
    n->prev = NULL;
    *h = n;
    *t = n;
} else {
    n->data = i;
    n->next = *h;
    n->next->prev = n;
    n->prev = NULL;
    *h = n;
    }}

主要

int main(){
...
struct node* A[M];

bool b;

for( i = 0; i < M; i++ ) {
    A[i] = NULL;
}
fillHashTable(hashtable);
.....
   //I allow user to make insert/delete element to the existing **item.list.text**

因此,在运行此程序后,我希望我可以显示 item list.tx t中的所有现有数据,并且数据已经被哈希处理。然后用户可以插入或删除该文本文件。

到目前为止,我在尝试编译时遇到了错误

  

没有匹配的呼叫功能   `HashTableSeparateChaining :: insert(node **,node **,Item&amp;)'

1 个答案:

答案 0 :(得分:1)

使用一些修补程序编译代码后,我确定问题是私有node结构。编译器错误澄清了一切:

25:6: note: void HashTableSeparateChaining::insert(HashTableSeparateChaining::node**, HashTableSeparateChaining::node**, Item)
25:6: note:   no known conversion for argument 1 from 'fillHashTable(HashTableSeparateChaining)::node**' to 'HashTableSeparateChaining::node**'

正如我在评论中所说的那样nodeHashTableSeparateChaining类的私有本地结构,所以你不应该像在第3段中那样在这个类之外使用它。通过将其移到外面或公开并通过HashTableSeparateChaining::node进行推荐来开始修复。它将解决这个问题。但还有更多。 fillHashTable不通过引用接受参数,并在循环内创建本地hashtable变量。恕我直言,代码应如下所示:

void fillHashTable(HashTableSeparateChaining& hashtable){
ifstream file;
file.open("item list.txt");
if(!file) {
    cout<<" Error opening file. " << endl;}

int item_id;
double price;
string item_name;

Item i;
struct node* A[M];

while(file >> item_id >> price >> item_name)
{
    i.setItem_id(item_id);
    i.setPrice(price);
    i.setItem_name(item_name);

   int hv = hash_function( i.getItem_id());       
   hashtable.insert(&A[hv], &A[hv], i);
}
file.close();}

类:

struct node
{
      struct node *next;//SINGLY-LINKED LIST
      struct node *prev;
      Item data;
};

class HashTableSeparateChaining{
public:

    void insert(struct node **h, struct node **t, Item i);
    void Delete(struct node **h, struct node **t, int i);
    void display( struct node* h );
    void search( struct node *h, int key );                          
};