码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-09 12:35:21      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:散列   开放定址法   数据结构   

头文件:

#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;
}
/**************************************************************************/



版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:散列   开放定址法   数据结构   

原文地址:http://blog.csdn.net/u012000209/article/details/47374457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!