Hash function

This is a discussion on Hash function within the C Programming forums, part of the General Programming Boards category; Hi to all! I want to make a hash table using a hash function. I do: Code: #include<stdio.h> #include<stdlib.h> #define ...

  1. #1
    g_p
    g_p is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  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, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21