散列技术之链地址法(基于无序链表)

源代码如下:

#include <stdlib.h>
#include <stdio.h>
#define hash(v,M) (v % M)
typedef char Key;
struct Item{
	Key key;
};
typedef struct STnode* link;
struct STnode{
	Item item ; link next;
};


static link* heads , z ;
static struct Item NULLitem ;
static int N , M ;


Key key(Item item){
	return item.key;
}
//创建一个节点 
static link NEW(Item item, link next){
	link x = (link)malloc(sizeof *x);
	x->item = item;x->next = next;
	return x;
}
//初始化 
void STinit(int max){
	int i ;
	N = 0; M = max / 5;
	heads = (link *)malloc(M*sizeof(link));
	z = NEW(NULLitem,NULL);
	for(i=0;i<M;i++)heads[i] = z;
}
//节点个数 
int STcount(){
	return N; 
} 
//搜索子程序 
Item searchR(link h, Key v){
	Key t = key(h->item);
	if(h==z)return NULLitem;
	if(v==t) return h->item;
	return searchR(h->next,v);
}
//搜索主程序 
Item STsearch(Key v){
	return searchR(heads[hash(v,M)],v);
}


//插入
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值