Algorithms: 散列表

/*---------------------------------------------------------------------------------------
 * Project: HashQuad.h
 * Name: zwp
 * Date: 2014.3
 *----------------------------------------------------------------------------------------*/




#ifndef HASHQUAD_H_
#define HASHQUAD_H_



typedef int ElementType;


typedef unsigned int Index;
typedef Index Position;


struct HashTbl;
typedef struct HashTbl* HashTable;


/*
** 初始化哈希表
*/
HashTable InitializeTable(int TableSize);

/*
** 遍历散列表
*/
void Traverse(HashTable H);


/*
** 销毁哈希表
*/
void DestroyTable(HashTable H);


/*
** 寻找元素
*/
Position Find(ElementType Key, HashTable H);

/*
** 插入元素
*/
void Insert(ElementType Key, HashTable H);

/*
** 删除元素 
*/
ElementType Retrive(Position P, HashTable H);

/*
** 二次散列
*/
HashTable Rehash(HashTable H);





#endif
/*--------------------------------------------------------------------------
 * Project: HashQuad.cpp
 * Name: zwp
 * Date: 2014/3
 *---------------------------------------------------------------------------*/


#include "HashQuad.h"
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

/*
** 表示三种状态, 已在, 空, 删除 
*/
enum KindOfEntry { Legitimate, Empty,  Deleted };

#define MinTableSize 10

struct HashEntry
{
	ElementType  Element;
	enum KindOfEntry Info;
};


typedef struct HashEntry Cell;

struct HashTbl
{
	Cell *TheCells;
	int TableSize;

};




/*
** 判断是不是素数
*/
bool isPrime(int number)
{
	number = abs(number);

	if(number == 0 || number == 1)
		return true;
	
	int divisor;
	for(divisor = number/2; number % divisor != 0; -- divisor)
		;
	return divisor == 1;
}



/*
** 产生素数
*/
static int NextPrime(int num)
{
	for(int index = num; index < 10000; ++ index)
	{
		if(num)
			return num;
	}
	return num;
}


/*
** 散列函数
*/
int Hash(ElementType key, int size)
{
	return (key % size);
}



/*
** 初始化散列表
*/
HashTable InitializeTable(int TableSize)
{
	HashTable H;
	int	i;

	if(TableSize < MinTableSize)
	{
		printf("Table size too small....\n");
		return NULL;
	}
	/*
	** Allocate Table
	*/
	H = (HashTbl*)malloc(sizeof(struct HashTbl));
	if(H == NULL)
		printf("Out of space.....\n");
	H->TableSize = NextPrime(TableSize);

	/* Allocate array of Cells */
	H->TheCells = (Cell*)malloc(sizeof(Cell) * H->TableSize);
	if(H->TheCells == NULL)
		printf("Out of space....\n");

	/* Set all number state is ‘Empty’ */
	for(i = 0; i < H->TableSize; i ++)
		H->TheCells[i].Info = Empty;

	return H;

}

/*
** 寻找元素
*/
Position Find(ElementType Key, HashTable H)
{
	Position CurrentPos;
	int		CollisionNum = 0;

	CurrentPos = Hash(Key, H->TableSize);
	while(H->TheCells[CurrentPos].Info != Empty && H->TheCells[CurrentPos].Element != Key)
	{
		/* Probably need strcmp ReHash */
		CurrentPos += 2 * (++CollisionNum) - 1;

		if(CurrentPos >= H->TableSize)
			CurrentPos -= H->TableSize;
	}

	return CurrentPos;
	
}


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 Retrive(Position P, HashTable H)
{
	ElementType number = 0;

	if(H->TheCells[P].Info == Legitimate)
	{
		number = H->TheCells[P].Element;
		H->TheCells[P].Info = Empty;
	}
	return number;
}



/*
** 遍历散列表
*/
void Traverse(HashTable H)
{
	if(H->TheCells == NULL)
		printf("The Hash Table is Empty...\n");

	for(int index = 0; index < H->TableSize; ++ index)
		printf("%d  \n", H->TheCells[index].Element);


}


/*
** 销毁哈希表
*/
void DestroyTable(HashTable H)
{
	free(H->TheCells);
	free(H);

}


/*
** 二次散列
*/
HashTable Rehash(HashTable H)
{
	int i, OldSize;
	Cell* OldCells;

	OldCells = H->TheCells;
	OldSize = H->TableSize;

	/* Get a new, empty table  */
	H = InitializeTable(2 * OldSize);

	/* Scan through old table, reinsering into new */
	for(i = 0; i < OldSize; ++ i)
		if(OldCells[i].Info == Legitimate)
			Insert(OldCells[i].Element, H);

	free(OldCells);

	return H;

}


/*--------------------------------------------------------------------
 * Project: Main.cpp
 * Name: zwp
 * Date: 2014/3
 *--------------------------------------------------------------------*/



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



int main(int argc, char* argv[])
{

	HashTable H;

	H = InitializeTable(100);

	for(int i = 0; i < 100; ++ i)
		Insert(i+2, H);

	Traverse(H);


	system("pause");


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值