散列表的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;
}
/**************************************************************************/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章