数据结构再学习-散列表(哈希表)实现

#include 
#include 

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
#define SUCCESS true
#define UNSUCCESS false
#define HASHSIZE 12
#define NULLKEY -32768

typedef int Status;

typedef struct
{
	int *elem;
	int count;
}HashTable;

int m=0;

Status InitHashTable(HashTable *H)
{
	int i;
	m=HASHSIZE;
	H->count=m;
	H->elem=(int *)malloc(m*sizeof(int));
	for(i=0;ielem[i]=NULLKEY;
	return OK;


}

int Hash(int key)
{
	return key % m;

}

void InsertHash(HashTable *H,int key)
{
	int addr=Hash(key);
	while(H->elem[addr]!=NULLKEY)
		addr=(addr+1)%m;
	H->elem[addr]=key;
}

Status SearchHash(HashTable H,int key,int *addr)
{
	*addr=Hash(key);
	while(H.elem[*addr]!=key)
	{
		*addr=(*addr+1)%m;
		if(H.elem[*addr]==NULLKEY||*addr==Hash(key))
			return UNSUCCESS;
	}
	return SUCCESS;

}

int main(int argc,char *argv[])
{
	int arr[HASHSIZE]={12,67,56,16,25,37,22,29,15,47,48,34};
	int i,p,key,result;
	HashTable H;

	key=39;

	InitHashTable(&H);
	for(i=0;i

你可能感兴趣的:(数据结构&&算法)