散列查找-平方探测法

  • 解决散列表的冲突问题有两种办法:开放地址法和链地址法

下面的代码是采用开放地址法,平方探测时的代码。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

#define MAXTABLESIZE 100

typedef int ElementType;

//散列单元状态类型
typedef enum
{
    Legitimate,  //有合法元素
    Empty, //空单元
    Deleted  //有已删除元素
}EntryType;

struct HashEntry  //散列表的单元类型
{
    ElementType Data;  //存放元素
    EntryType Info;  //单元状态
};

struct TblNode  //散列表的节点定义
{
    int TableSize;  //表的最大长度
    struct HashEntry* Cells;  //存放散列单元数据的数组
};

//返回大于N且不超过MAXTABLESIZE的素数
int NextPrime(int N)
{
    int i;
    int p = (N % 2) ? N + 2 : N + 1;  //从大于N的下一个奇数开始

    while (p <= MAXTABLESIZE)
    {
        for (i = (int)sqrt(p); i > 2; i--)
        {
            if (!(p % i))
            {
                break;  //p不是素数
            }
        }
        if (i == 2)
        {
            break;
        }
        else
        {
            p = p + 2;
        }
        
    }

    return p;
}

struct TblNode* CreateTable(int TableSize)
{
    struct TblNode* Hash;
    int i;

    Hash = (struct TblNode*)malloc(sizeof(struct TblNode));
    Hash->TableSize = NextPrime(TableSize);  //保证散列表的最大长度是一个素数
    //声明单元组
    Hash->Cells = (struct HashEntry*)malloc(Hash->TableSize * sizeof(struct HashEntry));
    //初始化单元状态为空单元
    for (int i = 0;i < Hash->TableSize;i++)
    {
        Hash->Cells[i].Info = Empty;
    }

    return Hash;
}

int GetHashPos(ElementType Key,int TableSize)
{
    return Key % 11;
}
//
int Find(struct TblNode* Hash,ElementType Key)
{
    int CurrentPos;
    int NewPos;
    int CNum = 0;  //记录冲突次数

    NewPos = CurrentPos = GetHashPos(Key,Hash->TableSize);  //初始散列位置

    while (Hash->Cells[NewPos].Info != Empty &&
           Hash->Cells[NewPos].Data != Key)
    {
        //统计1次冲突 并判断奇偶次
        if (++CNum % 2)  //奇数次冲突
        {
            //增量为 +[(CNum+1)/2]^2
            NewPos = CurrentPos + (CNum + 1) * (CNum + 1) / 4;
            if (NewPos >= Hash->TableSize)
            {
                NewPos = NewPos % Hash->TableSize;  //调整为合法地址
            }
        }
        else  //偶数次冲突
        { 
            NewPos = CurrentPos - CNum * CNum / 4;
            while (NewPos < 0)
            {
                NewPos = NewPos + Hash->TableSize;  //调整为合法地址
            }
        }
    }
    //此时NewPos或者是Key的位置,或者是一个空单元的位置(表示找不到)
    return NewPos;
}
//将元素插入哈希表
bool Insert(struct TblNode* Hash,ElementType Key)
{
    int  pos = Find(Hash,Key);

    if (Hash->Cells[pos].Info != Legitimate)
    {
        //如果这个单元格没有被占,说明key可以插入在此
        Hash->Cells[pos].Data = Key;
        Hash->Cells[pos].Info = Legitimate;
        return true;
    }
    else
    {
        printf("键值已存在.\n");
        return false;
    }

}

int main()
{
    int a[] = {47,7,29,11,9,84,54,20,30};
    //这里传入的值是9,但创建的HashTable的size并不是9,而是11
    struct TblNode* HashTable = CreateTable(9);

    for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
    {
        if (!Insert(HashTable, a[i]))
        {
            printf("插入元素失败.\n");
        }
    }
    printf("插入元素成功.\n");
    //访问元素
    for (int i = 0; i < HashTable->TableSize; i++)
    {
        if (HashTable->Cells[i].Info == Legitimate)
        {
            printf("%d  %d.\n", i, HashTable->Cells[i].Data);
        }
    }
    system("pause");
    return 0;
}
  • 运行结果

1327401-20190824163103669-443397842.png

  • 参考资料
    1 《数据结构(第2版)》 陈越主编 高等教育出版社

转载于:https://www.cnblogs.com/Manual-Linux/p/11404654.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值