哈希链表的实现C++

两大类,一类是结点的定义,一类是哈希表的定义:

具体的算法实现可以参考下面的程序:

.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型建立的哈希表,哈希函数相对比较简单,对字符串等其他类型,需要改动程序。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值