Hash Array Index Search

This is a discussion on Hash Array Index Search within the C Programming forums, part of the General Programming Boards category; I have a hashAry. I want to fill it with data from a file using hash function modulus division. Can ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    23

    Hash Array Index Search

    I have a hashAry. I want to fill it with data from a file using hash function modulus division. Can I search for the index in the hashAry until it matches with the address from modulus division?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You just want to search the hash table?
    Code:
    /* Search a table of strings */
    char *search ( const char *find )
    {
      int i = hash ( find );
    
      while ( hash_table[i] != NULL ) {
        if ( strcmp ( hash_table[i], find ) == 0 )
          return hash_table[i];
        else
          i = ( i + 1 ) % max_elements;
      }
    
      return NULL;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    23
    What if the hash array is an array of nodes?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What if the hash array is an array of nodes?
    Just add an extra level of indirection for the comparison.
    Code:
    struct node search ( const char *find )
    {
      int i = hash ( find );
    
      while ( hash_table[i] != NULL ) {
        if ( strcmp ( hash_table[i].something, find ) == 0 )
          return hash_table[i];
        else
          i = ( i + 1 ) % max_elements;
      }
    
      return empty_node;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  2. Replies: 41
    Last Post: 07-04-2004, 04:23 PM
  3. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 08:31 AM
  4. More efficient: BST or Hash search??
    By aspand in forum C Programming
    Replies: 4
    Last Post: 11-19-2002, 07:42 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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