哈希表(二)(散列)开放定址法(平方)

编译环境:vs2015


函数主体:


结构体:

代码:

// dataStructure-HashTable(2).cpp : Defines the entry point for the console application.
// hashTable-开放定址散列表

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define  ERROR  printf("error!") ;
typedef int ElementType;
struct HashEntry;
struct HashT;
typedef struct HashEntry Cell;
typedef struct HashT *HashTable;

//函数主体
//初始化
HashTable InitializeTable(int size);
//销毁
void destory(HashTable Htable);
//查找
int  findByKey(int key, HashTable Htable);
//插入
void insert(int key, HashTable Htable);
//删除
void deleteT(int key, HashTable Htable);
//遍历
void visited(HashTable Htable);
//返回位置
int Hash(int key, HashTable Htable);
//质数
int nextPrime(int size);

enum statusCode {
	Legitimate, Empty, Deleted
};

struct HashEntry
{
	ElementType element;
	enum statusCode status;
};
struct HashT
{
	int TableSize;
	Cell * TheCells;
};
int main()
{
	int size = 10;
	HashTable Htable = InitializeTable(size);
	for (int i = 0; i < size; i++)
	{
		insert(i*i, Htable);
	}
	deleteT(1,Htable);
	visited(Htable);
	destory(Htable);
	visited(Htable);
	return 0;
}

//初始化
HashTable InitializeTable(int size)
{

	HashTable Htable = (struct HashT *)malloc(sizeof(struct HashT));
	if (Htable == NULL) { printf("1"); ERROR; }
	Htable->TableSize = nextPrime(size);
	Htable->TheCells = (struct HashEntry*)malloc(Htable->TableSize * sizeof(struct HashEntry));
	if (Htable == NULL) { printf("2"); ERROR; }
	for (int i = 0; i < Htable->TableSize; i++)
	{
		Htable->TheCells[i].element = i;
		Htable->TheCells[i].status = Empty;
	}
	return Htable;
}
//销毁
void destory(HashTable Htable)
{

	free(Htable->TheCells);
	Htable->TheCells = NULL;
	free(Htable);
	Htable = NULL;
}
//查找
int  findByKey(int key, HashTable Htable)
{
	if (Htable == NULL) { printf("3"); ERROR; }
	int flag = Hash(key, Htable);
	int i = 0;
	while (Htable->TheCells[flag].status == Deleted || Htable->TheCells[flag].status == Legitimate)
	{
		//走到下一个格子
		//f(x)=x^2 , f(x-1)=(x-1)^2  f(x)-f(x-1)=2*x-1;
		flag = flag + 2 * (++i) - 1;
		printf("%d\n",flag);
		//超出表格,重头来
		if (flag >= Htable->TableSize)
		{
			flag = flag - Htable->TableSize;
		}
	}
	return flag;
}
//插入
void insert(int key, HashTable Htable)
{
	if (Htable == NULL) { printf("4"); ERROR; }
	Cell * cell = &(Htable->TheCells[findByKey(key, Htable)]);
	cell->element = key;
	cell->status = Legitimate;
}
//删除
void deleteT(int key, HashTable Htable)
{
	if (Htable == NULL) { printf("5"); ERROR; }
	Cell * cell = &(Htable->TheCells[findByKey(key, Htable)]);
	cell->status = Deleted;
}
//遍历
void visited(HashTable Htable)
{
	if (Htable == NULL) { printf("6"); ERROR; }
	for (int i = 0; i < Htable->TableSize; i++)
	{
		//if (Htable->TheCells[i].status == 0)
		{
			printf("%d\t%d\n", Htable->TheCells[i].element, Htable->TheCells[i].status);
		}
		
	}
}

int Hash(int key, HashTable Htable)
{
	if (Htable == NULL) { printf("7"); ERROR; }
	int newKey = key%Htable->TableSize;
	return newKey;
}


//接近的下一个质数
int nextPrime(int x)
{
	int flag = 0;
	for (int i = 2 * x; flag == 0; i++)
	{
		int j = 2;
		for (; j <= sqrt(i); j++)
		{
			if (i%j == 0)
			{
				break;
			}
		}
		if (j == (int)sqrt(i) + 1)
		{
			flag = i;
		}
	}
	return flag;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值