J
joenchinghkg
I am going to create an method to insert Items inside the hash table,
However, i don't really know what I should code inside the method.
Should I use push or pop because it is based on a list container for my
private attribute.
Here is my code.
hashtable.h
Code:
//HashTable.h
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <list>
using namespace std;
template <typename KEYTYPE, typename VALUETYPE>
class HashTable
{
public:
void insertItem( const string& key, const VALUETYPE& value
);
VALUETYPE getItem( const string& key ) const;
void dump() const;
private:
enum { TABLESIZE = 20 };
struct KeyValuePair {
KEYTYPE key;
VALUETYPE value;
};
list<KeyValuePair> hashTable[ TABLESIZE ];
unsigned getIndex( const char* key ) const; // Hash Function
};
#include "hashtable.tem"
#endif
hashtable.tem
Code:
//hashtable.tem
template <typename KEYTYPE, typename VALUETYPE>
void HashTable<KEYTYPE,VALUETYPE>::insertItem( const string& key, const
VALUETYPE& value ) {
}
template <typename KEYTYPE, typename VALUETYPE>
void HashTable<KEYTYPE,VALUETYPE>::dump() const {
for( unsigned iCtr = 0; iCtr < TABLESIZE; iCtr++ ) {
cout << "Bucket #" << iCtr << " contains: ";
list<KeyValuePair>::const_iterator iter;
for( iter = hashTable[ iCtr ].begin(); iter != hashTable[ iCtr
].end(); ++iter ) {
cout << '[' << iter->key.c_str() << ',' << iter->value << ']'
<< ' ';
}
cout << endl;
}
}
// End of file

Thanks for advice
However, i don't really know what I should code inside the method.
Should I use push or pop because it is based on a list container for my
private attribute.
Here is my code.
hashtable.h
Code:
//HashTable.h
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <list>
using namespace std;
template <typename KEYTYPE, typename VALUETYPE>
class HashTable
{
public:
void insertItem( const string& key, const VALUETYPE& value
);
VALUETYPE getItem( const string& key ) const;
void dump() const;
private:
enum { TABLESIZE = 20 };
struct KeyValuePair {
KEYTYPE key;
VALUETYPE value;
};
list<KeyValuePair> hashTable[ TABLESIZE ];
unsigned getIndex( const char* key ) const; // Hash Function
};
#include "hashtable.tem"
#endif
hashtable.tem
Code:
//hashtable.tem
template <typename KEYTYPE, typename VALUETYPE>
void HashTable<KEYTYPE,VALUETYPE>::insertItem( const string& key, const
VALUETYPE& value ) {
}
template <typename KEYTYPE, typename VALUETYPE>
void HashTable<KEYTYPE,VALUETYPE>::dump() const {
for( unsigned iCtr = 0; iCtr < TABLESIZE; iCtr++ ) {
cout << "Bucket #" << iCtr << " contains: ";
list<KeyValuePair>::const_iterator iter;
for( iter = hashTable[ iCtr ].begin(); iter != hashTable[ iCtr
].end(); ++iter ) {
cout << '[' << iter->key.c_str() << ',' << iter->value << ']'
<< ' ';
}
cout << endl;
}
}
// End of file
Thanks for advice