I'd like to get feedback on whether my hash table is correct or not. Also, any ideas on performance?
template<class J,class K>
class List {
private:
struct Node {
J key;K data;
Node * next;
Node(J _key,K _data):key(_key),data(_data),next(nullptr) {}
}*head;
public:
List():head(nullptr){}
~List() {
deleteNode();
}
void insert(J _key ,K _data) {
Node * newNode = new Node(_key, _data);
newNode->next = head;
head = newNode;
}
void traverse() {
for(Node * curr = head; curr != nullptr; curr = curr->next) {
cout<< "( "<<curr->key << ", " << curr->data << " )" << " ";
}
}
K search(J key) {
if (!empty()){
for(Node * curr = head;curr != nullptr;curr = curr->next) {
if (curr->key == key) {
return curr->data;
}
}
}
return K(-1);
}
bool empty() { return (head == nullptr); }
void deleteNode() {
Node * tmp = nullptr;
for(Node * curr = head; curr != nullptr;){
tmp = curr->next;
delete curr;
curr = tmp;
}
}
};
template<class KEY, class VALUE>
class myHash {
private:
struct bucket {
List<KEY,VALUE> list_at_bucket;
}*Buckets;
int tableSize;
public:
myHash(int size) {
tableSize = size;
Buckets = new bucket[tableSize];
}
~myHash() {
for(int i = 0; i < tableSize; i++) {
Buckets[i].list_at_bucket.deleteNode();
}
}
void put(KEY k, VALUE value) {
int getCode = hash(k);
Buckets[getCode].list_at_bucket.insert(k,value);
}
VALUE getValue(KEY _key) {
int getCode = hash(_key);
return Buckets[getCode].list_at_bucket.search(_key);
}
bool contains(KEY _key) {
int getCode = hash(_key);
if (Buckets[getCode].list_at_bucket.search(_key) != -1) {
return true;
}
return false;
}
// need a template hash function
int hash(KEY key) const {
int value = 0;
for (char each: key) {
value += each;
}
return value % tableSize;
}
void traverse() {
for (int index = 0; index <tableSize; index++){
cout<< "[" << index << " ]-> ";
Buckets[index].list_at_bucket.traverse();
}
}
};
-
\$\begingroup\$ @jamal the code works \$\endgroup\$mello– mello2017年02月03日 02:48:48 +00:00Commented Feb 3, 2017 at 2:48
-
\$\begingroup\$ You're also asking some off-topic questions, particularly ones regarding adding new code. \$\endgroup\$Jamal– Jamal2017年02月03日 02:50:42 +00:00Commented Feb 3, 2017 at 2:50
-
\$\begingroup\$ are you talking about the comment in the code ? \$\endgroup\$mello– mello2017年02月03日 02:51:43 +00:00Commented Feb 3, 2017 at 2:51
-
\$\begingroup\$ Yes, and some below the code, such as making keys unique and implementing a generic hash function. \$\endgroup\$Jamal– Jamal2017年02月03日 02:53:15 +00:00Commented Feb 3, 2017 at 2:53
-
\$\begingroup\$ i removed all comments \$\endgroup\$mello– mello2017年02月03日 02:56:37 +00:00Commented Feb 3, 2017 at 2:56
1 Answer 1
Well, there's a lott to say:
K
andV
are the traditional template-arguments for keys and values in C++.
Why do you useJ
andK
respectivelyKEY
andVALUE
instead?List::Node
is only instantiated at one place, where all three members are set. Why then does it have a constructor at all, and one setting the last one tonullptr
at that?Default copy- and move- construction and assignment are completely unsuited to your
List
, as it owns its nodes. Leaving them leads to double-deletes and abandoned nodes. Look at the rule of three.Also worth a look are inclass-initializers (for
head
).deleteNode
looks more likereset
, and should zerohead
or be inlined into the destructor. As-is, it leaveshead
dangling...The same comments apply to copying / moving / assigning
myHash
as toList
.The
myHash
dtor leaks the array of buckets...contains
should return the condition directly, instead of putting it into an if-clause.if(somebool) return true; else return false;
is a bit wordy.Your
traverse
-functions would be more appropriately nameddump
or some such... Anyway, why don't you overloadoperator>>(std::ostream, yourtypehere)
instead?Your
hash
-function runs afool of the fact that only the type itself can know which parts are relevant and how to hash them. Take a look at the design ofstd::hash
.Next, I would suggest embracing
auto
, see Almost Always Auto.
-
\$\begingroup\$ can i add the changes onto the original question ? so you could tell me if i have done them right \$\endgroup\$mello– mello2017年02月06日 17:15:21 +00:00Commented Feb 6, 2017 at 17:15
Explore related questions
See similar questions with these tags.