C语言平方探测法题目,开放定址散列表(平方探测法)实现文件C语言

本文介绍了一种使用开放定址法实现散列表的方法,并提供了详细的源代码解析。包括散列表初始化、查找、插入、删除及遍历等操作的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*open_addressing_hash(2) -- 开放定址散列表实现文件*/

#include

#include

#include "open_addressing_hash(2).h"

/*局部函数声明*/

static int Get_Prime_Value (const int size) ;

static int Is_An_Prime (const int number) ;

static int Square_ (const int i) ;

/*接口函数定义*/

int Hash (const int item, const int size)

{

return item % size ;

}

int InitializeTable (HashTable * const ph, const int size)

{

int temp, count ;

(*ph) = (struct hashtable *) malloc (sizeof (struct hashtable)) ;

if (NULL == *ph)

{

puts ("Out of space[1].") ;

return 0 ;

}

temp = (*ph) -> size = Get_Prime_Value (size) ;

(*ph) -> lists = (Cell *) malloc (sizeof (Cell) * temp) ;

if (NULL == (*ph) -> lists)

{

free (*ph) ;

puts ("Out of space.[2]") ;

return 0 ;

}

for (count = 0; count < temp; count++)

(*ph) -> lists[count].entry = Empty ;

return 1 ;

}

Cell * Find (const HashTable * const ph, const Item item)

{

int key, size, critical, value, index, i = 0 ;

size = (*ph) -> size ;

key = Hash (item, size) ;

critical = size / 2 + 1 ;

do

{

if (i <= critical)

{

value = Square_ (i++) ;

index = (key + value) % size ;

}

else

break ;

}

while (Legitimate == (*ph) -> lists[index].entry && (*ph) -> lists[index].item != item)

;

return (*ph) -> lists + index ;

}

int Insert (const HashTable * const ph, const Item item)

{

Cell * position ;

position = Find (ph, item) ;

if (Empty == position -> entry || Deleted == position -> entry)

{

position -> entry = Legitimate ;

position -> item = item ;

return 1 ;

}

return 0 ;

}

int Delete (const HashTable * const ph, const Item item)

{

Cell * position ;

position = Find (ph, item) ;

if (Legitimate == position -> entry && item == position -> item)

{

position -> entry = Deleted ;

return 1 ;

}

return 0 ;

}

void Traversal (const HashTable * const ph, void (* pfun) (const Cell cell))

{

int count, size = (*ph) -> size ;

for (count = 0; count < size; count++)

(* pfun) ((*ph) -> lists[count]) ;

}

void Release (const HashTable * const ph)

{

free ((*ph) -> lists) ;

free (*ph) ;

}

/*局部函数定义*/

static int Get_Prime_Value (const int size)

{

int real_size = size ;

while (!Is_An_Prime (real_size))

real_size++ ;

return real_size ;

}

static int Is_An_Prime (const int number)

{

int count, value ;

if (number < 2)

{

puts ("Wrong input.") ;

return 0 ;

}

for (count = 2, value = number / 2 + 1; count < value; count++)

if (0 == number % count)

return 0 ;

return 1 ;

}

static int Square_ (const int i)

{

return i * i ;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值