哈希之线性探测法

数据结构实验之查找七:线性之哈希表
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

根据给定的一系列整数关键字和素数p,用除留余数法定义hash函数H(Key)=Key%p,将关键字映射到长度为p的哈希表中,用线性探测法解决冲突。重复关键字放在hash表中的同一位置。

Input

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

Output

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

Example Input

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

Example Output

1 1 1 1 1
4 0 1 3
4 0 1 2
4 0 1 2 0

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int ans[1015];//存元素在哈希表中的位置
int a[1015];
int main()
{
    int n, p, k, top, num, i;
    while(~scanf("%d %d", &n, &p))
    {
        top = 0;
        memset(a, -1, sizeof(a));
        for(i = 0; i < n; i++)
        {
            scanf("%d", &num);//输入一个数
            k = num % p;//该数对p求余
            while(a[k] != -1 && a[k] != num)//判断哈希表这个位置是不是有数,如果有数就向后移动一个位置,直到该位置没有存数为止
            {
                k = (k + 1) % p;//向后移动一个位置
            }
            if(a[k] == -1)//代表位置是空的
            {
                a[k] = num;//直接存入
                ans[top++] = k;//记录下标
            }
            else if(a[k] == num)//代表数一样
            {
                ans[top++] = k;//记录下标
            }
        }
        for(i = 0; i < top; i++)
        {
            printf("%d", ans[i]);
            if(i != top - 1) printf(" ");
            else printf("\n");
        }
    }
    return 0;
}
根据提供的引用内容,我们可以了解到线性探测法是散列表中的一种解决冲突的方法,但是容易造成元素聚集,导致元素无法均匀存入表中,增加了运算的复杂度。因此,我们可以使用平方探测法来解决这个问题。 以下是Java中平方探测法实现散列表的代码示例: ```java public class MyHash { private DataItem[] hashArray; private int arraySize; private DataItem nonItem; public MyHash(int size) { arraySize = size; hashArray = new DataItem[arraySize]; nonItem = new DataItem(-1); } public void displayTable() { System.out.print("Table: "); for (int j = 0; j < arraySize; j++) { if (hashArray[j] != null) { System.out.print(hashArray[j].getKey() + " "); } else { System.out.print("** "); } } System.out.println(""); } public int hashFunc(int key) { return key % arraySize; } public void insert(DataItem item) { int key = item.getKey(); int hashVal = hashFunc(key); int stepSize = 1; while (hashArray[hashVal] != null && hashArray[hashVal].getKey() != -1) { hashVal += stepSize * stepSize; hashVal %= arraySize; stepSize++; } hashArray[hashVal] = item; } public DataItem delete(int key) { int hashVal = hashFunc(key); int stepSize = 1; while (hashArray[hashVal] != null) { if (hashArray[hashVal].getKey() == key) { DataItem temp = hashArray[hashVal]; hashArray[hashVal] = nonItem; return temp; } hashVal += stepSize * stepSize; hashVal %= arraySize; stepSize++; } return null; } public DataItem find(int key) { int hashVal = hashFunc(key); int stepSize = 1; while (hashArray[hashVal] != null) { if (hashArray[hashVal].getKey() == key) { return hashArray[hashVal]; } hashVal += stepSize * stepSize; hashVal %= arraySize; stepSize++; } return null; } } class DataItem { private int iData; public DataItem(int ii) { iData = ii; } public int getKey() { return iData; } } ``` 以上代码实现了平方探测法的散列表,其中包括插入、删除和查找操作。在插入操作中,我们使用平方探测法来解决冲突,即每次增加的步长为1的平方,直到找到空的位置为止。在删除和查找操作中,我们也使用相同的探测方式来查找对应的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值