Thread: Hash function

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Hash function

    Hi to all!

    I want to make a hash table using a hash function.

    I do:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define array_size 100
    
    struct hello {
    
    char *name;
    struct hello *next;
    
    };
    
    int number = 0;
    
    int hash_function(char *name,int number)
    
    {
    
    int x = number % array_size;
    
    number++;
    
    return x;
    
    
    
    }
    
    
    int main() {
    
    int l = hash_function("Good morning",number);
    printf("%d\n",l);
    
    }
    Has anyone to suggest me another hash funtion using the char *name as a key and not an integer like number, in order an index of the hash table to be produced?
    Because the one i have made it's not a hash funtion

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The hash result computes some value based on the data.
    Code:
    int notAVeryGoodHash ( const char *str ) {
      int result = 0;
      while ( *str ) {
        result += *str++;
      }
      return result &#37; array_size;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM