目前,我正在尝试将二叉树转换为哈希表,使用单独的链式算法在DEV++中开发一个简单的程序。
到目前为止,我已经转换了insert、delete、search和display操作函数以及哈希函数。
项目list.txt:
Item ID Item Price Item Name
10001 23.00 Book1
10002 24.00 Book2
10003 31.98 Book3
10004 41.90 Book4我陷入困境的部分是,我不知道如何将所有的项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;
}}main:
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(节点**,节点**,Item&)‘的调用没有匹配函数
发布于 2015-11-24 09:14:36
在用一些补丁编译代码之后,我确信这个问题是私有的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**'正如我在注释中所指出的,node是HashTableSeparateChaining类的私有本地结构,因此您不应该能够像在第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 );
};https://stackoverflow.com/questions/33888709
复制相似问题