hashtable - C programming question on the implementation of a hash table -


I have a C programming question on the implementation of the hash table. I have applied the hash table to store some stars. I'm having a problem while working with hash coliies. I'm following a series of link-list approach to fight the problem but, somehow, my code is behaving differently, I'm able to debug it. I'm not. Can anyone help?

This is what I am facing: Say for the first time, I put a string called Gaur. Calculates my hash map index as 0 and incorporates the string successfully. However, when the hash of any other string is calculated, then 0 becomes, my previous value is overwritten i.e. Gaur will be replaced by a new string.

This is my code:

  structure list {char * string; Structure list * next; }; Structure hash_table {int size; / * Table size * / structure list ** table; / * Table element * /}; Struct hash_table * create_hash_table (integer shape) {struct hash_table * new_table; Int i; If (size & lt; 1) return tap; / * Invalid size for table * / / * Attempt to allocate memory for table structure / if ((new_table = malloc (sizeof (struct hash_table))) == NULL {return zero;} / * Memory for table Attempt to assign * / if ((new_table- & gt; table = mauloq (size (structure list *) * size)) == faucet {return tap;} / * start elements of table * / I = 0; i & lt; size; i ++) new_table- & gt; table [i] = '\ 0'; / * set the size of the table * / new_table- & gt; shape = size; new returned; Unsigned f Numeric hash (strat hash_table * hashtable, four * str) {for unsigned intestine hasle = 0; int i = 0; (; * str! = '\ 0', str ++) {hash = = str [i]; I ++;} return (hasht = hashtable-> size);} structure list * lookup_string (structure hash_table * hashtable, char * str) {printf (enters "\ n lookup_string \ n"); * New_list; unsigned int hashval = hash (hashtable, str); / * Depending on the hash value go to the correct list and see that the list is * str.If this is so, then back in the list element one Uck Return. * If it is not, then the item is not in the table, so return zero * / (new_list = hashtable- & gt; table [hashval]; new_list! = NULL; new_list = new_list-> Next) {If ( Strcmp (str, new_list-> string) == 0) return new_list; } Printf ("returns \ n lookup_string returns \ n"); Return tap; } Int add_string (Structured hash_table * hashtable, char * str) {printf ("enters \ n add_string \ n"); Structure list * new_list; Structure list * current_list; Unsigned int hashval = hash (hashtable, str); Printf ("\ nHashil =% d", hashval); / * Attempts to allocate memory to list * / if ((new_list = malloc (size (structure list)) == tap) {printf ("\ n enters here \ n"); return1;} / * What item already exists? * / Current_list = Lookup_string (hashtable, str); if (current_list == faucet) {printf ("\ n DEBUG purpose \ n"); Printf ("\ n NULL \ n");} / * Item already exists, do not include it again. * / If (current_list! = NULL) {printf ("\ n object already exists ... \ n"); Return 2;} / * In the list Insert * / printf ("Entering \ n ... \ n"); New_list-> String = stdup (str); New_list-> gt; next = null; // new_list-> gt = next = hashmelate-> table [hashawl]; if (hashtable-> table [hashal] == faucet) -> table [hashval] = new_list;} other {struct list * temp_list = hashtable-> table [hashawal]; while (temp_list-> next! = NULL) temp_list = temp_list-> Next; Temp_list- & Gt; next = new_list; hashtable-> table [hashval] = new_list;} return 0;}  

I have not checked to confirm, but this line looks incorrect:

  Hashtable-> Table [hashal] = new_list;  

It's okay at the end of the last case of add_string :

  • Correct to keep the value being added The new structure list is created
  • The linked list was correctly detected for that hash
  • At the end of the written list
  • Li>

    , the row I quoted above, you can add head to the linked list for New structure list to tell the hash table Sector are Hashvalue! This way the entire link list was thrown before.

    I think you should leave the line that I quoted above, and see how you get it. The preceding lines are adding it correctly at the end of the existing list.


Comments