两大类,一类是结点的定义,一类是哈希表的定义:
具体的算法实现可以参考下面的程序:
.hpp文件
#pragma once
#include<iostream>
#include<ctype.h>
using namespace std;
typedef int Elemtype;
const int DEFAULT_SIZE=5;//如果没有指定桶的个数,就按默认值5
class Lnode
{
public:
bool IsEmpty()
{
if (this->next== nullptr)
return true;
else
return false;
}
public:
Lnode* next;
int key;
Elemtype data;
};
class Hash_Table
{
public:
Hash_Table();
int Hash_fun(Elemtype value);//定义哈希函数
void Create_HashTable(Elemtype* value, int size);//创建哈希表
bool unique_Lnode(Elemtype value); //判断结点是否重复
void Insert_Lnode(Elemtype value);//插入结点进入哈希表
void Debug();//测试一下
~Hash_Table();
private:
Lnode*table;
int num;
};
Hash_Table::Hash_Table()
{
this->num = DEFAULT_SIZE;
cout << "输入哈希桶的个数:"; cin >>this-> num;
if (num <= 0)
this->num = DEFAULT_SIZE;
table = new Lnode[num];
for (int i = 0; i < this->num; i++)
{
this->table[i].data = NULL;
this->table[i].key=i;
this->table[i].next = nullptr;
}
}
int Hash_Table::Hash_fun(Elemtype value)
{
return (value % this->num);
}
void Hash_Table::Create_HashTable(Elemtype* value,int size)
{
for (int i = 0; i < size; i++)
{
int _key = this->Hash_fun(value[i]);
if (table[_key].IsEmpty())
{
Lnode* node = new Lnode;
node->data = value[i]; node->key = _key; node->next = nullptr; table[_key].next = node;
}
else//如果现在结点不为空,先去遍历,看是否该元素已经添加
{
bool reslut= this->unique_Lnode(value[i]);//reslut为1:该元素为新元素,为0,就不再添加了
if (reslut)
{
Lnode* node = new Lnode;
node->data = value[i]; node->key = _key; node->next = table[_key].next; table[_key].next = node;
}
else
continue;
}
}
}
bool Hash_Table::unique_Lnode(Elemtype value)//判断数据是否已经存在
{
int index = this->Hash_fun(value);
Lnode *temp = table[index].next;
while (temp)
{
if (temp->data == value)
return false;
temp = temp->next;
}
return true;
}
void Hash_Table::Insert_Lnode(Elemtype value)
{
int reslut = this->Hash_fun(value);
if (this->table[reslut].next==nullptr)
{
Lnode* node = new Lnode;
node->data = value; node->key = reslut; node->next = nullptr; table[reslut].next = node;
}
else//如果现在结点不为空,先去遍历,看是否该元素已经添加
{
bool reslut1 = this->unique_Lnode(value);//reslut为1:该元素为新元素,为0,就不再添加了
if (reslut1)
{
Lnode* node = new Lnode;
node->data = value; node->key = reslut; node->next = table[reslut].next; table[reslut].next = node;//前插法
}
else
return;
}
}
void Hash_Table::Debug()
{
cout << "测试如下:" << endl;
for (int j = 0; j < this->num; j++)
{
Lnode *temp = this->table[j].next;
cout << "索引为" << j << "的数值:";
if (temp == nullptr)
{
cout << "NULL" << endl;
continue;
}
while (temp)
{
cout << temp->data << "\t";
temp = temp->next;
if (!temp)
cout << endl;
}
}
}
Hash_Table::~Hash_Table()//尤其要注意开辟在堆区的数据的删除
{
for (int j = 0; j < this->num; j++)
{
Lnode* p1,*p2;
p1 = this->table[j].next;
p2 = p1;
while (p1)
{
p2 = p1->next;
delete p1;
p1 = p2;
}
this->table[j].next = nullptr;
}
delete[] this->table;
cout << "释放空间成功!" << endl;
}
main函数:
#include<iostream>
#include"哈希表.hpp"
using namespace std;
void text()
{
Elemtype ch[] = { 54,596,45,12,56,47,12,59,45,33,33,21,45,96,14,96,77,54,44,66,23,44 };
int length = sizeof(ch) / sizeof(ch[0]);
Hash_Table H1;
H1.Create_HashTable(ch, length);
H1.Debug();
cout << "--------------------------------------" << endl;
H1.Insert_Lnode(888);
H1.Debug();
cout << "--------------------------------------" << endl;
}
int main()
{
text();
system("pause");
return 0;
}
测试的结果:
讨论:
这里主要是针对int型建立的哈希表,哈希函数相对比较简单,对字符串等其他类型,需要改动程序。