I'm trying to implement a HashTable. In fact, I want a HashSet with Persons that have a name and a phoneNumber, both string. I understood the theory with collisions and everything but I don't know how to really implement something. This is what I've done so far:
#ifndef HASHSET_H_
#define HASHSET_H_
#include <iostream>
using namespace std;
#include <cstdio>
template <typename Element>
class HashSet{
private:
class Node{
private:
Element info;
Node* next;
public:
Node(){ //constructor
this->next=NULL;
}
Node(Node* next, Element info){ //parameters constructor
this->info=info;
this->next=next;
}
Node(const Node& node){ //copy constructor
this->info=node.info;
this->next=node.next;
}
~Node(){} //destructor
Element getInfo(){
return this->info;
}
Node* getNext(){
return this->next;
}
void setInfo(Element newE){
this->info=newE;
}
void setNext(Node* value){
this->next=value;
}
};
Node** head;
int hashSize;
int bucketSize; //holds the total elements of a cell
int totElems; //total number of elements in the hashset
public:
HashSet(){
}
bool IsEmptyAtGivenKey(int key);
bool HashSetIsFull();
int HashFunction(string e);
int HashSetSize();
};
template<typename Element>
HashSet<Element>::HashSet(){
this->hashSize=11;
this->head=new Node*[hashSize];
for(int i=0; i< hashSize; i++){
this->head[i]=NULL;
}
this->totElems=0;
}
template<typename Element>
bool HashSet<Element>::IsEmptyAtGivenKey(int key){
if(key>=0 and key < hashSize){
return head[key]==NULL;
}
return true;
}
template<typename Element>
int HashSet<Element>::HashFunction(string e){
int length = e.size();
int sum;
for(int i=0; i < length; i++){
sum+=e[i];
}
int hashCode = sum % hashSize;
return hashCode;
}
#endif /* HASHSET_H_ */
Is it any good? Any advice that will help me improve is welcome!
std::vector
here. \$\endgroup\$ – Quentin Pradet May 6 '13 at 12:27