【发布时间】:2015-11-24 08:28:41
【问题描述】:
目前我正在尝试使用分离链算法将二叉搜索树转换为哈希表,以便在 DEV-C++ 中开发一个简单的程序。
到目前为止,我已经转换了插入、删除、搜索和显示操作函数,以及哈希函数。
项目列表.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.txt 中的所有现有数据以及已经被散列的数据。然后用户可以对该文本文件进行插入或删除。
到目前为止,我在尝试编译时遇到了这个错误。
没有匹配的调用函数 `HashTableSeparateChaining::insert(node**, node**, Item&)'
【问题讨论】:
-
在哪里你得到错误?这是编译器的唯一输出吗?
-
什么是
Item类型? -
@JoachimPileborg 在
hashtable.insert(&A[hv], &A[hv], i);处出错。程序的输出取决于用户选项。 eg:输入1表示插入,2表示删除,3表示显示 -
@Jerome Item 是一个具有 item_id、price 和 item_name 的对象
-
node是HashTableSeparateChaining类的私有本地结构,因此您不应该像在 3rd sn-p 中那样在此类之外使用它。将它移到外面开始你的修复或制作public并通过HashTableSeparateChaining::node引用
标签: c++ algorithm hashtable chaining