散列表的C语言实现-开放定址法

头文件:

#ifndef __HASHTABLE_H
#define __HASHTABLE_H


/*********************(平方)开放定址散列法***************/

//如果有冲突发生,那么就尝试另外的单元,直到找到空的单元为止
typedef unsigned int index;
typedef index position;
typedef int ElementType;
#define MINTABLESIZE 5

struct hashTable;
typedef struct HashTable *hashTable;

hashTable initializeTable(int tableSize);
void destoryTable(hashTable H);
position find(ElementType key,hashTable H);
void insert(ElementType key,hashTable H);
ElementType retrieve(position p,hashTable H);
hashTable reHash(hashTable H);
int Delete(ElementType key,hashTable H);
int nextPrime(int tableSize);
int isPrime(int x);
int Hash(ElementType key,int tableSize);

enum kindOfEntry{legitimate,empty,deleted};

struct hashEntry
{
	ElementType element;
	enum kindOfEntry info;
};

typedef struct hashEntry cell;

struct HashTable
{
	int tableSize;
	cell *theCells;
};
#endif


实现文件:

#include "HashTable.h"
#include <stdio.h>
#include <stdlib.h>

/***********************开发定址法(平方)***********************************/
hashTable initializeTable(int tableSize)
{
	hashTable table;
	int i;
	if(tableSize<MINTABLESIZE)
	{
		printf("table size is too small\n");
		exit(-1);
	}
	table=(hashTable)malloc(sizeof(struct HashTable));
	if(!table)
	{
		printf("out of space\n");
		exit(-1);
	}
	table->tableSize=nextPrime(tableSize);
	table->theCells=(cell*)malloc(sizeof(cell)*table->tableSize);
	if(!table->theCells)
	{
		printf("out of space\n");
		exit(-1);
	}
	for(i=0;i<table->tableSize;i++)
		table->theCells[i].info=empty;
	
	return table;
}

void destoryTable(hashTable H)
{
	free(H->theCells);
	H=NULL;
}

position find(ElementType key,hashTable H)
{
	position currentPos;
	int collisionNum;
	
	collisionNum=0;
	currentPos=Hash(key,H->tableSize);
	while(H->theCells[currentPos].info!=empty&&
		H->theCells[currentPos].element!=key)
	{
		//平方探测法
		currentPos+=2*collisionNum-1;
		if(currentPos>=H->tableSize)
			currentPos-=H->tableSize;
	}
	return currentPos;
}

int Delete(ElementType key,hashTable H)
{
	position pos;
	pos=find(key,H);
	if(H->theCells[pos].info==empty)
		return 0;
	else
	{
		H->theCells[pos].info=deleted;
		return 1;
	}
}

void insert(ElementType key,hashTable H)
{
	position pos;
	pos=find(key,H);
	if(H->theCells[pos].info!=legitimate)
	{
		H->theCells[pos].info=legitimate;
		H->theCells[pos].element=key;
	}
}

ElementType retrieve(position p,hashTable H)
{

}

hashTable reHash(hashTable H)	//再散列
{
	int i,oldSize;
	cell *oldCells;

	oldCells=H->theCells;
	oldSize=H->tableSize;

	H=initializeTable(2*oldSize);
	for(i=0;i<oldSize;i++)
	{
		if(oldCells[i].info==legitimate)
			insert(oldCells[i].element,H);
	}
	free(oldCells);
	return H;
}
int nextPrime(int tableSize)
{
	while(1)
	{
		if(isPrime(tableSize))
			return tableSize;
		else
			tableSize++;
	}
}
int isPrime(int x)
{
	int i;
	for(i=2;i*i<=x;i++)
	{
		if(x%i==0)
			return 0;
	}
	return 1;
}
int Hash(ElementType key,int tableSize)
{
	return key%tableSize;
}
/**************************************************************************/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值