功能要求:
程序部分:
1.显示交互界面
2.能够通过键盘的输入控制程序的退出
哈希表部分:
1.能够显示所用的哈希算法
2.能够添加数据单元
3.能够检索已有的全部数据单元
4.若果采取动态内存分配,则能够释放所用的内存
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//*****创建一个允许n个地址数的哈希表*****
//假设要存储的数据是int类型
typedef int data_type;
//定义(链表节点)数据单元的数据类型
typedef struct link_node
{
data_type data;
struct link_node * pNext;
}link_node;
//定义哈希表头的数据类型,该结构体存放哈希表的首地址
typedef struct hashtable_head
{
link_node ** pArr;
// int count;
}ht_head; //现在还没有表的空间
void menu()
{
putchar('\n');
printf("*****Hash Table*****\n");
printf("0--------Quit\n");
printf("1--------Check the hash_func\n");
//创建哈希表1的表头
*pht1_head = (ht_head*)malloc(sizeof(ht_head));
if(*pht1_head==NULL)
{
printf("malloc failed!\n");
return -1;
}
(*pht1_head)->pArr = NULL;
printf("The head of hash table 1 has been initialized.\n");
printf("2--------Create\n");
printf("3--------Insert\n");
printf("4--------Search\n");
printf("5--------!!!DESTROY!!!\n");
printf("*****Hash Table*****\n");
}
//哈希函数
int hash_func(int key,int n)
{
return key % n;
}
int create(ht_head ** pht1_head,link_node *** pht1,int n)
{
//创建哈希表1
*pht1 = (link_node**)malloc(n * sizeof(link_node));
if(*pht1==NULL)
{
printf("malloc failed!\n");
return -1;
}
//此处的 *pht1 是一个指针数组,这里是指针数组的初始化。
int i;
for(i=0;i<n;i++)
{
(*pht1)[i] = NULL;
}
printf("The hash table 1 has been initialized.\n");
//创建哈希表1的表头
*pht1_head = (ht_head*)malloc(sizeof(ht_head));
if(*pht1_head==NULL)
{
printf("malloc failed!\n");
return -1;
}
//要让哈希表1的表头链上哈希表体
(*pht1_head)->pArr = *pht1;
printf("The head of hash table 1 has been initialized.\n");
return 0;
}
int insert(ht_head * pht1_head,link_node ** pNew,data_type x,int n)
{
if(pht1_head == NULL)
{
printf("The hash table doesn't exist!\n");
return -1;
}
//开辟pNew对应的空间
*pNew = (link_node *)malloc(sizeof(link_node));
if(*pNew==NULL)
{
printf("malloc pNew failed!\n");
return -1;
}
//初始化pNew
memset(&(*pNew)->data,0,sizeof(data_type));//数据域写空
(*pNew)->pNext = NULL;//指针域置空
(*pNew)->data = x;//数据域写值
//获取数据在哈希表中的位置
int pos = hash_func(x,n);
//重链接
(*pNew)->pNext = pht1_head->pArr[pos]; //保护节点
pht1_head->pArr[pos] = *pNew;//链接
putchar('\n');
printf("insert completed!\n");
return 0;
}
int search(ht_head * pht1_head,data_type x,int n)
{
if(pht1_head==NULL)
{
printf("null error!\n");
return -1;
}
//先通过哈希函数求得数据在表的哪个位置
int pos = hash_func(x,n);
//创建一个搜寻节点的指针变量
link_node * pFind = NULL;
//将它指向数据链表的第一处
pFind = pht1_head->pArr[pos];
//开始搜寻
while(pFind!=NULL)
{
if(pFind->data == x)
{
printf("The data exists in the hash table.\n");
return 0;
}
pFind = pFind->pNext;
}
printf("The data doesn't exist in the hash table.\n");
return -1;//走到这里说明没找到
}
//需要对 ht_head * 类型的指针指向的哈希表表头
//以及 link_node ** 类型的指针指向的哈希表
//进行空间的释放,所以把它们的地址传进来,所以加一个*
int destroy(ht_head ** pht1_head,link_node *** pht1,int n)
{
if(*pht1_head==NULL)
{
printf("The hash table doesn't exist!\n");
return -1;
}
link_node * pDel = NULL;
pDel = (*pht1_head)->pArr[0];//pDel先指向哈希表0号单元上链表的首节点
int i=0;
//此处即为哈希表空间的释放。
//先释放数据的存储空间
for(;i<n;i++)
{
while(pDel!=NULL) //头删
{
pDel = (*pht1_head)->pArr[i];//pDel先指向链表的首节点
//创建哈希表1的表头
*pht1_head = (ht_head*)malloc(sizeof(ht_head));
if(*pht1_head==NULL)
{
printf("malloc failed!\n");
return -1;
}
(*pht1_head)->pArr = NULL;
printf("The head of hash table 1 has been initialized.\n");
(*pht1_head)->pArr[i] = pDel->pNext;//保护后面的节点
free(pDel);
pDel = NULL;
}
//释放每一个哈希表单元的空间
free((*pht1_head)->pArr[i]);
(*pht1_head)->pArr[i] = NULL;
}
//哈希表的空间已空,
//那么还需要把原来指向哈希表的指针置空 并 释放里面对应的空间
free(*pht1_head);
*pht1_head = NULL;
free(*pht1);
*pht1 = NULL;
printf("destroy completed!\n");
return 0;
}
int main()
{
ht_head * pht1_head = NULL; //哈希表1的表头指针
link_node ** pht1 = NULL; //哈希表1的指针
int n = 0; //哈希表存放地址数的最大值
data_type x = 0; //想要增添的数据
link_node * pNew = NULL;//增添单元的缓冲区
int op = 0;
printf("How many addresses the hashtable shall contain?\n");
scanf("%d",&n);
while(1)
{
menu();
printf("Please input the option number:\n");
scanf("%d",&op);
switch(op)
{
case 0:
printf("Exited!\n");
return 0;
case 1:
printf("hash(key)=key rem %d\n",n);
break;
case 2:
create(&pht1_head,&pht1,n);
break;
case 3:
printf("Please input the data you want to put in the hash table:\n");
scanf("%d",&x);
if(search(pht1_head,x,n)<0)
{
insert(pht1_head,&pNew,x,n);
}
break;
case 4:
printf("Please input the data you want to search in the hash table:\n");
scanf("%d",&x);
search(pht1_head,x,n);
break;
case 5:
destroy(&pht1_head,&pht1,n);
break;
default:
printf("Please check your option number!\n");
break;
}
}
return 0;
}
结果演示:
销毁效果展示: