I'm just experimenting with hash tables since I never build them before, but I'll keep that in mind to not restrict it to hold a certain type (hockey_player). Does anyone know what causes my trivial sc_put (
separate
chaining put/insert) function to not compile when I call it?
For my second post, I'm building a hash table via separate chaining using linked lists ( I read it can be done with trees since its using pointers but I'll stick w/ linked lists for now).
My hash table insert (i.e. sc_put) function seems like a trivial implementation using separating chaining, but I can't figure out what's wrong with the code preventing it from running. My hash table insert function simply inserts @ front of list btw. As you can see (from the struct in first post), I'm using a doubly linked lists. Can someone also explain why using a dll (doubly linked list) is more efficient than a sll (singly linked list). I know there is extra "work" to have to keep track of a previous pointer in order to use it to "link past" the current pointer like in a delete operation, but isn't there the obvious overhead of the memory occupied by need to reserve space for a back pointer for each dll node? In addition, for bidirectional, it's possible w/ a sll to just keep track of a tail pointer so two pointers (head pointer and tail pointer) and tail pointer will initially point to what head pointer points to when only one node in list, but > 1 nodes, only head pointer gets updated (assuming simply insert @ front of list (not ordered in other words) ).
Sorry to be longwinded, but I know performance is based on the load factor: num elems/table size, BUT is is num elems
for a given bucket/table size
OR is it
ALL elems/table size that dictates performance?
Here is my bool is_duplicate function and it's not working too as I commented out my sc_put function (hash table insert in other words) and the compiler complains as such:
hash_fcn_and_tables.cpp: In function 'bool is_duplicate(hockey_player*, int, int
, int, std::string, std::string)':
hash_fcn_and_tables.cpp:50: error: no match for 'operator==' in '*(bucket + ((un
signed int)(((unsigned int)cur_bucket) * 20u))) == 0'
hash_fcn_and_tables.cpp:53: error: cannot convert 'hockey_player' to 'hockey_pla
yer*' in initialization
Note: below I have placed the code lines from my actual file, why does it complain about the operator '==' which I am using properly in the if statement below...
1 2 3 4 5 6 7 8 9 10 11
|
/*47*/bool is_duplicate(hockey_player* bucket, int cur_bucket, int size, int num_, string name_, string team_)
/*48*/{
/*49*/ bool isDuplicate = false;
/*50*/ if ( bucket[cur_bucket] == NULL )//if list is empty, of course not duplicate
/*51*/ return isDuplicate;
/*52*/
/*53*/ for ( hockey_player* cur = bucket[cur_bucket]; cur != NULL; cur = cur->next )/**Else traverse bucket to find if itm to insert is a duplicate or not*/
/*54*/ if ( cur->num == num_ && cur->name == name_ && cur->team == team_ )
/*55*/ return isDuplicate && false;
/*56*/ return isDuplicate;
/*57*/}
|
I have checked online and it seems the implementations are either using an existing library like STL maps for C++ or Java's built in, but I would like to learn to practice building trivial hash table via separate chaining in this case just as I realize linked lists also have libraries but I can easily create singly and doubly linked lists etc.
I hope someone can take a look, the code is very trivial for a hash table implementation and that's all I want, to get experience before using C++ STL maps for eg.