Hash解决冲突之线性探测

数据结构实验之查找七:线性之哈希表
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
根据给定的一系列整数关键字和素数p,用除留余数法定义hash函数H(Key)=Key%p,将关键字映射到长度为p的哈希表中,用线性探测法解决冲突。重复关键字放在hash表中的同一位置。

输入
连续输入多组数据,每组输入数据第一行为两个正整数N(N <= 1000)和p(p >= N的最小素数),N是关键字总数,p是hash表长度,第2行给出N个正整数关键字,数字间以空格间隔。

输出
输出每个关键字在hash表中的位置,以空格间隔。注意最后一个数字后面不要有空格。

示例输入
5 5
21 21 21 21 21
4 5
24 15 61 88
4 5
24 39 61 15
5 5
24 39 61 15 39
示例输出
1 1 1 1 1
4 0 1 3
4 0 1 2

4 0 1 2 0

感悟:1理解题意不充分就会写出有bug的程序.读好题真的很重要

        2有时问题暂时解决不了千万不要钻牛角尖一直去琢磨,放一放较好.

# include <stdio.h>
int main()
{
    int Hash[1999];//记录数据按hash计算出的位置,并存入该数据
    int a[1009];
    int N,P;
    int i,j,k,pos;
    while((scanf("%d%d",&N,&P))!=EOF)//输入总数,最小质数
    {
        for(i=0;i<P;i++)
        {
            Hash[i] = -1;
        }
        for(i=0;i<N;i++)
        {
            scanf("%d",&a[i]);
            pos = a[i]%P;
           /* //wrong!!!  忽视了同余的这种情况,没有在Hash数组搜索a[i]是否已存在
            if(Hash[pos] == -1 || Hash[pos] == a[i]) //5 11
            {                                       //7 18 7 18 29
                Hash[pos] = a[i];                   //7 8 7 8 10
            }
            */
            if(Hash[pos] == -1) //该位置没有占用
            {
                Hash[pos] = a[i];
            }
            else
            {
                //在Hash数组中进行搜索,看a[i]是否已存在
                for(j=0;j<P;j++)
                {
                    if(Hash[j] == a[i])//已存在
                    {
                        break;//跳出,数字无需重复存
                    }
                }
                if( j == P)//Hash数组中不存在a[i]
                {
                    for(k=1;k<P;k++)//线性探测
                   {
                        pos = (a[i] + k) % P;
                        if(Hash[pos] == -1)
                        {
                            Hash[pos] = a[i];
                            break;
                        }
                    }
                }

            }
        }
        /* 按输入顺序查找每个元素在hash表中的位置*/
        for(i=0;i<N;i++)
        {
            pos = a[i]%P;
            if(Hash[pos] == a[i])
            {
                if(i!=N-1)
                    printf("%d ",pos);
                else
                    printf("%d\n",pos);
            }
            else
            {
                /*Hash线性搜索元素*/
                for(j=1;j<P;j++)
                {
                    pos = (a[i] + j)%P;
                    if(Hash[pos] == a[i])
                    {
                         if(i!=N-1)
                            printf("%d ",pos);
                         else
                            printf("%d\n",pos);
                         break;
                    }
                }
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的线性探测法哈希表的实现(C语言): ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 1000 struct hash_node { char *key; char *value; }; struct hash_table { struct hash_node *table[TABLE_SIZE]; }; unsigned int hash_function(const char *key) { unsigned int hash = 0; int i; for (i = 0; i < strlen(key); i++) { hash += key[i]; hash += (hash << 10); hash ^= (hash >> 6); } hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); return hash % TABLE_SIZE; } struct hash_table *hash_create() { struct hash_table *hash = malloc(sizeof(struct hash_table)); int i; for (i = 0; i < TABLE_SIZE; i++) { hash->table[i] = NULL; } return hash; } void hash_destroy(struct hash_table *hash) { int i; for (i = 0; i < TABLE_SIZE; i++) { struct hash_node *node = hash->table[i]; if (node != NULL) { free(node->key); free(node->value); free(node); } } free(hash); } void hash_set(struct hash_table *hash, const char *key, const char *value) { unsigned int slot = hash_function(key); struct hash_node *node = hash->table[slot]; int i = 0; while (node != NULL) { if (strcmp(node->key, key) == 0) { free(node->value); node->value = strdup(value); return; } i++; slot = (slot + i) % TABLE_SIZE; node = hash->table[slot]; } node = malloc(sizeof(struct hash_node)); node->key = strdup(key); node->value = strdup(value); hash->table[slot] = node; } char *hash_get(struct hash_table *hash, const char *key) { unsigned int slot = hash_function(key); struct hash_node *node = hash->table[slot]; int i = 0; while (node != NULL) { if (strcmp(node->key, key) == 0) { return node->value; } i++; slot = (slot + i) % TABLE_SIZE; node = hash->table[slot]; } return NULL; } int main() { struct hash_table *hash = hash_create(); hash_set(hash, "name", "John"); hash_set(hash, "age", "30"); printf("Name: %s, Age: %s\n", hash_get(hash, "name"), hash_get(hash, "age")); hash_destroy(hash); return 0; } ``` 这个哈希表使用了一个简单的哈希函数,可以根据需要进行修改。在这个哈希表中,冲突的时候使用了线性探测法来解决
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值