Thread: hash tables

  1. #1
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24

    hash tables

    hey there!
    could anyone of you c gurus please explain me how a hash table is implemented? a friend of mine said that it would save two things, a char and the value of that char and when the char apeared in the file it would be automaticly replaced by its value. is that so?

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    hash table is an associative data structure - check this link:

    http://www.hprog.org/fhp/HashTable
    .sect signature

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A hash table at its simplest is just an array. The hash function turns whatever your key is into an index for the array and that is where you store the item. The problem is that a hash function rarely gives every key a unique index, so hash tables aren't always simple arrays. They either have ways of choosing a different index that can be easily and reproducibly probed later, or multiple items are placed at the same index in a linked list or other dynamic data structure. Here is a quick and dirty implementation of the latter:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define N 53
    
    struct hl_hnode {
      char data[BUFSIZ];
      struct hl_hnode *next;
    };
    
    struct hl_hnode *table[N];
    
    unsigned hash ( const char *s )
    {
      unsigned h;
    
      for ( h = 0; *s != 0; s++ )
        h = *s + 37 * h;
    
      return h % N;
    }
    
    char *hl_probe ( const char *key )
    {
      struct hl_hnode *it;
    
      for ( it = table[ hash ( key ) ]; it != NULL; it = it->next ) {
        if ( strcmp ( it->data, key ) == 0 )
          return it->data;
      }
    
      return NULL;
    }
    
    void hl_insert ( const char *key )
    {
      unsigned h;
      struct hl_hnode *new_node;
    
      /* Disallow duplicates */
      if ( hl_probe ( key ) != NULL )
        return;
      /* Hash data */
      h = hash ( key );
      if ( ( new_node = malloc ( sizeof *new_node ) ) == NULL )
        return;
      strcpy ( new_node->data, key );
      new_node->next = table[h];
      table[h] = new_node;
    }
    My best code is written with the delete key.

  4. #4
    anguished incognito54's Avatar
    Join Date
    Apr 2004
    Posts
    24
    hmmm!!!
    that seems just perfect for the current project i have in hands, a different kind of c pre-processor
    thanks a lot you guys!!
    I've never felt the nausea of longing to feel nothing,
    I never wanted to cease to exist, just disappear...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iterable hash tables
    By OnionKnight in forum Tech Board
    Replies: 5
    Last Post: 07-21-2008, 02:02 AM
  2. developing hash tables in the C Programming language
    By w108dab in forum C Programming
    Replies: 1
    Last Post: 05-20-2008, 11:20 AM
  3. need help with hash tables with direct chaining
    By agentsmith in forum C Programming
    Replies: 4
    Last Post: 01-05-2008, 04:19 AM
  4. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  5. Problems with Hash Tables
    By Zildjian in forum C Programming
    Replies: 6
    Last Post: 11-06-2003, 08:53 PM