Reputation: 383
I am trying to build a Hash table and with what I have learned using online tutorials I have come up with the following code
#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;
}
I am trying to insert string values and validate if the name exists using the retrieveItem function. Also, how do I go about printing the HashTable with values.
Any help will be highly appreciated.
Vish
Upvotes: 0
Views: 4935
Reputation: 384
just use stl hashmap or for threadsafe hashmap use intel thread building blocks library
Upvotes: 0
Reputation: 490603
There's quite a bit wrong here. Just glancing at a random spot in the middle of the code, I found:
Node::Node(string) {
data = "";
next = NULL;
}
This doesn't save the value of the string that was passed in. I'm guessing you really wanted something more like:
Node::Node(string value) : data(value), next(NULL) {}
The next thing I saw was:
int Node::hash(int value) {
int y;
y = value % SIZE;
}
This computes a value and puts it into y
(a local variable) but doesn't return anything, so the value you've computed is immediately lost. If you attempt to use its return value, you get undefined behavior (actually, I don't remember for sure -- it seems like in C you get UB only if you try to use the return value, but in C++ it's UB to do this at all -- regardless of the technical details, though, it's clearly not what you want and needs to be fixed).
Like I said, I just glanced at one spot in the code and found though -- I doubt they're the only problems. The bottom line is that you probably need to back up a bit, figure out what each function is supposed to do, and go through them one by one and ensure each is doing what it's supposed to.
One other bit of advice: instead of dealing with dynamically allocating an array on your own, I'd used std::vector
.
Upvotes: 1