我正在尝试构建一个哈希表,并根据我使用在线教程学到的知识,我想出了以下代码
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int SIZE = 100;
int hash(string);
class Node
{
public:
Node();
Node(string);
int hash(int value);
private:
string data;
Node* next;
friend class HashTable;
};
Node::Node() {
data = "";
next = NULL;
}
Node::Node(string) {
data = "";
next = NULL;
}
int Node::hash(int value) {
int y;
y = value % SIZE;
}
class HashTable {
public:
HashTable();
HashTable(int);
~HashTable();
void insertItem(string);
bool retrieveItem(string);
private:
Node* ht;
};
HashTable::HashTable() {
ht = new Node[SIZE];
}
HashTable::HashTable(int max) {
ht = new Node[max];
}
HashTable::~HashTable() {
delete[] ht;
}
void HashTable::insertItem(string name) {
int val = hash(name);
for (int i = 0; i < name.length(); i++)
val += name[i];
}
bool HashTable::retrieveItem(string name) {
int val = hash(name);
if (val == 0 ) {
cout << val << " Not Found " << endl;
}
else {
cout << val << "\t" << ht->data << endl;
}
}
void print () {
//Print Hash Table with all Values
}
int main() {
HashTable ht;
ht.insertItem("Allen");
ht.insertItem("Tom");
ht.retrieveItem("Allen");
//data.hash(int val);
//cout << ht;
system("pause");
return 0;
}
int hash(string val) {
int key;
key = val % SIZE;
}
我正在尝试插入字符串值并使用retrieveItem 函数验证名称是否存在。另外,我该如何去打印带有值的 HashTable。
任何帮助将不胜感激。
维什