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 trying to use a container to hold a front pointer in each bucket (index) to a list, but is it really necessary.Code:#include <iostream> using namespace std; struct hockey_player { hockey_player* next; hockey_player* back; int num;//NB: this is key (jersey number) string name; string team; }; bool is_duplicate(hockey_league* bucket, int cur_bucket, int size, int num_, string name_, string team_) { if ( bucket->front == NULL )//if list is empty, of course not duplicate return false; for ( hockey_player* cur = bucket->front; cur != NULL; cur = cur->next )/**Else traverse bucket to find if itm to insert is a duplicate or not*/ if ( cur->num == num_ && cur->name == name_ && cur->team == team_ ) return true; return false; } int main() { hockey_player* p_1_front = NULL;//pointer to the items in each bucket (ie. the address in each array index to a list) hockey_player* p_2_front = NULL; hockey_player* p_3_front = NULL; hockey_player* p_4_front = NULL; hockey_player* p_5_front = NULL; hockey_player* p_6_front = NULL; hockey_player* p_7_front = NULL; hockey_player* p_8_front = NULL; hockey_player* p_9_front = NULL; hockey_player* p_10_front = NULL; hockey_player* p_11_front = NULL; hockey_player* p_12_front = NULL; hockey_player* p_13_front = NULL; hockey_player* p_14_front = NULL; hockey_player* p_15_front = NULL; hockey_league* bucket[15] = {p_1_front, p_2_front, p_3_front, p_4_front, p_5_front, p_6_front, p_7_front, p_8_front, p_9_front, p_10_front, p_11_front, p_12_front, p_13_front, p_14_front, p_15_front}; int size_ = 15; hockey_player* newGuy = new hockey_player; newGuy->num = 99; newGuy->name = "Wayne Gretzky"; newGuy->team = "EDM"; newGuy->next = NULL; newGuy->back = NULL; int i = newGuy->num % size_; cout << i << endl; bucket[i]->front = newGuy; return 0; }
I mean:Anyways, I just need help for why my bool is_duplicate function that seems so trivial doesn't work.Code:struct hockey_league { hockey_player* front;//to keep track of our list };



2Likes
LinkBack URL
About LinkBacks



