trouble with hash function implementing

This is a discussion on trouble with hash function implementing within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to build a trivial hash function using division method with divisor as table size (e.g. key % ...

trouble with hash function implementing

Hi, I'm trying to build a trivial hash function using division method with divisor as table size (e.g. key % size ) with separate chaining using doubly linked list. I have gone over the code over and over and cannot see why my logic to create an array of pointers to type hockey_player doesn't work. I have checked around, do I have to use a struct as container to hold my nodes? I don't see why my way doesn't work.

I was aiming for that b/c I thought it was an array of pointers. So initially all are NULL, so then I would insert a new node into slot hash(key) where my key is for e.g. newGuy->num % size (trivial but just to get going w/ hash tables implementation). So since newGuy has key (i.e. newGuy->num) 99, it would be inserted into index: 9 so now the front is updated to reflect this:

Code:

bucket[i]->front = newGuy;

And as I am typing this late at night, I realized if all are NULL, NULL->front doesn't make sense so I'll need to fix this up.

Also, does it matter to have a container to hold each nodes so I mean:

>> And also, I need to insert keys into all slots, right, not just use the key to computer the slot to insert to I mean.

Well, after you hash either to retrieve a node or to put something in the table you're supposed to use the linked list to store all of the values in the bucket. You will want to match up the key element with something in the list.

Hi whiteflags, so I do have to have a struct to hold all the individual pointers to each bucket and related issue, is there something I'm doing wrong w/ passing the pointer of the array declared in main(called bucket) since the code seems so simple but it doesn't run.

I put the same code in main and it works, could it do w/ pointers to pointers that I need to pass if I decide to not use another struct to hold the individual head pointers to each bucket's list. This is the first time I have implemented a hash table via separate chaining (with doubly linked lists I'm using). I understand pointers enough and linked lists, but what could I be doing wrong. The code below copied from body of my above bool is_duplicate function runs that's why I'm concerned it's the parameter I'm passing. I'm not sure...

is there something I'm doing wrong w/ passing the pointer of the array declared in main(called bucket) since the code seems so simple but it doesn't run.

Yes there is. It's as you figured out: a pointer to pointer will work. The reasoning, if you remember, an array decays to a pointer to the first element. The type of the first element is hockey_player* not hockey_player, so if we keep that in mind, it explains why it didn't work before.

do I really need to have 2 structs.

You do not need to use two structures when you implement a hash table. A hash table is just an array of nodes. There are some organizational benefits to having two structs though. In particular I think the literate programming example makes the most sense when you do that. Since the hash function is a member of the hash table struct, you won't accidentally use the wrong hash function on the wrong table, and the size of the table is also right there when you go to resize any tables.

if ( *bucket[cur_bucket] == NULL )//if list is empty, of course not duplicate

Now there should be a syntax error, because even if you don't understand the full error, it says that there is no equality operator to use in this line yet you should be able to compare pointers.

The second error in this case is a big hint to what's going on in both cases. It is caused by this line, by the same expression:

Code:

for ( hockey_player* cur = *bucket[cur_bucket]; ...

What's on the right hand side of the assignment is clearly not a pointer.

Remember that using the subscript operator will dereference the pointer once. Using the subscript operator is correct here, but remember if the type is hockey_player** and you want to get hockey_player*, you only need to dereference once. Extra stars do not make pointers in expressions.

You're a life saver, whiteflags. I've been tearing my hair out trying to figure that out, that passing by ptrs to ptrs and also forgetting that subscript notation is just syntactic sugar for pointer arithmetic.

That is and always has been a horrible idea, as it forces the size of the array to be fixed and any code that uses such array references is forever incompatible with the STL, and several threads have been spent telling you such things.

I was merely throwing out the suggestion there, not necessarily implying that it can and should be used. Just a little FYI, if you will.