源代码如下:
#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);
}
//插入