sucinct example of hash table w/ chaining

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 14
Reputation: silentdragoon is an unknown quantity at this point 
Solved Threads: 0
silentdragoon's Avatar
silentdragoon silentdragoon is offline Offline
Newbie Poster

sucinct example of hash table w/ chaining

 
0
  #1
Jan 15th, 2008
Can someone point me towards a dead basic, complete example of a hash table utilising chaining? I've got a working hash function, I am just getting really frustrated with not understanding what I don't understand with the setting up of the hash table. All the examples on the web I've looked at I can't get working, or they seem so complicated; pages and pages of code seeing how well it works, and on various inputs, and I just want something dead simple and transparent, that will not crash when I give it a word.

My understanding is that for the hash table with chaining, you'll need a linked list data structure (which I have working fine) and another data structure, that holds each chain and also an index. So I have something like
  1. typedef struct l {
  2. char* data;
  3. int numfound;
  4. struct l *next;
  5. } listelem;

for my linked list, which seems to work well, but I am stuck on what to do for the other data structure. Should it be

[code]
typedef struct hashtable {
struct listelem chain;
int index;
} hashtable;

or is that way off base?

The problem I'm having is that I'm developing in Windows at the moment, and when it doesn't work it just crashes, with no inkling of what went wrong, whereas on linux it seems to be easier to catch errors. Or it gives me thousands of errors/warnings on compile, and I can't resolve them.

I've written about three or four insert functions now, using different ideas and bases, and none of them have worked even a little bit. My current m.o. for the insert function is planned thusly:

// 1. hash word, get address
// 2. compare to word in that address, if there is one
// 3. if there is one, check it until next is null
// 4. check by strcmp-ing the data segment and the input string
// 5. if there is no match, add it to the beginning of the chain
// 6. if there is a match, increment the numfound and break

Does that sound reasonable?

Ach. My project is due on Friday and I'm so far behind.. Please help me, good people! I am talking to my tutor tomorrow, hopefully I can get some of this resolved, but I am in a sorry state -- programming six hours a night after an exhausting day just isn't going well. No sleep = no energy = sloppy programming = staying up to fix errors = no sleep, etc.

sad face.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 307
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: sucinct example of hash table w/ chaining

 
0
  #2
Jan 16th, 2008
These are questions for Narue. You could have a look at this Tutorial she wrote : link

[edit] Looking futher below in the threadlist, I saw that you allready asked this question in this thread and got the same answer... Sorry 'bout that

Niek
Last edited by niek_e; Jan 16th, 2008 at 5:04 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 14
Reputation: silentdragoon is an unknown quantity at this point 
Solved Threads: 0
silentdragoon's Avatar
silentdragoon silentdragoon is offline Offline
Newbie Poster

Re: sucinct example of hash table w/ chaining

 
0
  #3
Jan 16th, 2008
Even that tutorial has too much going on, I need something far simpler -- if such a thing exists.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,813
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: sucinct example of hash table w/ chaining

 
0
  #4
Jan 16th, 2008
Here's as simple as it gets with separate chaining, with some rudimentary testing. I wrote it in a few minutes, so don't blame me if there are bugs.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct node {
  6. char *data;
  7. struct node *next;
  8. } node;
  9.  
  10. unsigned djb_hash ( const void *key, int len )
  11. {
  12. unsigned const char *p = key;
  13. unsigned h = 0;
  14. int i;
  15.  
  16. for ( i = 0; i < len; i++ )
  17. h = 33 * h ^ p[i];
  18.  
  19. return h;
  20. }
  21.  
  22. static int find ( const node *table[], int size, const char *key )
  23. {
  24. unsigned index = djb_hash ( key, strlen ( key ) ) % size;
  25. const node *it = table[index];
  26.  
  27. /* Find a matching key in the list, if any */
  28. while ( it != NULL && strcmp ( it->data, key ) != 0 )
  29. it = it->next;
  30.  
  31. return it != NULL;
  32. }
  33.  
  34. static int insert ( node *table[], int size, const char *key )
  35. {
  36. if ( !find ( table, size, key ) ) {
  37. /* Find the linked list we want to search */
  38. unsigned index = djb_hash ( key, strlen ( key ) ) % size;
  39.  
  40. node *new_node = malloc ( sizeof *new_node );
  41.  
  42. if ( new_node == NULL )
  43. return 0;
  44.  
  45. new_node->data = malloc ( strlen ( key ) + 1 );
  46.  
  47. if ( new_node->data == NULL )
  48. return 0;
  49.  
  50. /* Fill in the data and link to the front of the list */
  51. strcpy ( new_node->data, key );
  52. new_node->next = table[index];
  53. table[index] = new_node;
  54.  
  55. return 1;
  56. }
  57.  
  58. return 0;
  59. }
  60.  
  61. #include <assert.h>
  62.  
  63. int main ( void )
  64. {
  65. node *table[101] = {0};
  66.  
  67. assert ( insert ( table, 101, "test" ) == 1 );
  68. assert ( find ( table, 101, "test" ) == 1 );
  69. assert ( insert ( table, 101, "test" ) == 0 );
  70. assert ( find ( table, 101, "foo" ) == 0 );
  71.  
  72. return 0;
  73. }
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 14
Reputation: silentdragoon is an unknown quantity at this point 
Solved Threads: 0
silentdragoon's Avatar
silentdragoon silentdragoon is offline Offline
Newbie Poster

Re: sucinct example of hash table w/ chaining

 
0
  #5
Jan 17th, 2008
Very nice! Thanks so much, that is a lot easier to understand. You rule Narue!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC