散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表
给定表M,存在函数f(key),对任意给定的关键字值key,代入函数后若能得到包含该关键字的记录在表中的地址,则称表M为哈希(Hash)表,函数f(key)为哈希(Hash) 函数。
#define HASHSIZE 32    


//待存入表格数据

char *keywords[] = {
        "auto", "break", "case", "char", "const", "continue", "default", 
        "do",
        "double", "else", "enum", "extern", "float", "for", "goto", 
        "if",
        "int", "long", "register", "return", "short", "signed", "sizeof", 
        "static",
        "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
        "while"
};

char keybuf[HASHSIZE][10];
static char val_flag[HASHSIZE];//标致已占用存储单元


void ClearFlag()
{
    int i;
    
    for (i = 0;i < HASHSIZE;i++)
    {
        val_flag[i] = (HASHSIZE+1);//清标致位

    }
}

//哈希函数,从数据中抽出某个成员用于哈希值的计算

unsigned int hash(char *s)
{
    unsigned int hashval;
    int i = 0;

    for (hashval = 0; *s != '\0'; s++)
        hashval = *s + 31 * hashval;
    hashval = hashval % HASHSIZE; //计算下标


    while ((val_flag[hashval] != (HASHSIZE+1)) && (i<32))
    {
        i++;
        hashval = (hashval + i)%HASHSIZE;    //冲突处理,存储单元(下标)偏移

    }
    if (i<HASHSIZE)
    {
        printf("\n元素下标(%d): 冲突次数: %d -- ",hashval,i);
        val_flag[hashval] = hashval; //表示该单元被占用

        return hashval;
    }
    return -1;
}

int main(void) 
{
  int i, size, pos;

  size = sizeof(keywords) / sizeof(keywords[0]);//计算关键字数量

 
  //将数据存入哈希表

  ClearFlag(); 
  for(i = 0;i < size; i++)
     strcpy(keybuf[hash(keywords[i])],keywords[i]);

  //根据数据结构中某个成员作为索引值,查找对应数据

  ClearFlag(); 
  for(i = 0; i < size; i++)
  {
    pos = hash(keywords[i]);    
    printf("%-10s: %-3d\n", keybuf[pos], pos);
  }

  return 0;
}