-
Hash table - problem
This is my first attempt at a hash table data structure. My problem is how can i change the stuct Entry_t for the class Hash_map so that Entry wouldn't be invalidated type. Also, i get this error "error C4430: missing type specifier - int assumed" for my function pointer declaration...
Code:
typedef template< class Map>
class Hash_Map
{
private :
typedef struct Entry_t
{
Map * Object;
Entry_t * next;
string * Key;
int position;
}Entry * Primary[], * Seconadary[];
Entry * make_node(std::string, Map * map );
size_t Num;
size_t secondary_size;
unsigned int (*Hash_function) (const * char, int );
public:
Hash_Map( Entry & P , size_t & N );
Hash_Map( Hash_Map & Map_secondary);
// the default SuperFastHash comes from the included header file
Hash_Map( int & num, unsigned int(*Hash_funct)(const * char, int) = SuperFastHash);
~Hash_Map();
int insert(std::string key, Map & map);
int deletion(std::string key);
bool isFull();
Map * find(std::string key);
int Secondaryinsert(std::string key, Map & map);
Swap_Hash(int leftoff)
};
#endif
-
Well, I haven't reviewed the whole thing thoroughly, and I haven't compiled it, but there were a couple of things I noticed:
- Line #27 of Hash_file.cc has an incomplete statement and no semicolen.
- The implementation functions in Hash_file.cc have to be templated, eg:
Code:
template< class Map>
Hash_Map<Map>::Hash_Map( const Hash_Map & Map_secondary)
{
// ...
}
- Drop the typedef before the Hash_Map class declaration.
- If you're going to pass references to the functions, make them const
- You can't assign your member variable to the local variable "Array", as it will be destroyed when the function completes.
Code:
typedef template< class Map>
Entry * Hash_Map::make_node(std::string key, Map * map )
{
Entry * Cur,
Cur = new Entry;
Cur->Object = map;
Cur->Key = &key;
Cur->next = NULL;
return(Cur);
}
You're basically doing the same thing here. First of all, "key" is just a temporary, and second you really shouldn't be forming a pointer to the string the user passes in *anyway*. Just declare the member as a plain std::string.
- Your insert function looks confused. First of all, you don't need to iterate through the array. That is, after all, the whole purpose of using a hash function! Next, you're just overwriting the current node. You just need to grab the node at Primary[h], iterate to the end, and add the new node.
- Your delete function is equally broken. Do similar to the above method, but when/if you find a matching node, you'll need to point it's predecessor to the next node, and then delete the current one, if that makes sense.
Sorry, but that's all I've got time for right now. Fix those errors and you'll be a little closer to a correct solution, at least. :p
-
thanks for the help, I fixed most of the errors, but then I get a linker error " [Linker error] undefined reference to `Hash_Map<int>::Hash_Map(int)' " and the other members... when i made a Main test file
Code:
#include <iostream>
using namespace std;
#include "Hash_file.h"
int main(void)
{
Hash_Map<int> Numbers(3);
Numbers.insert("one", 1);
Numbers.insert("two", 2 );
Numbers.insert("Three",3);
cout<<LNumbers.find("one");
cin.get();
return 0;
}