编译环境: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;
}