c++hash实现(+ELF算法)

本文探讨了C++中如何实现简单的哈希函数及使用ELF(拓展线性探测再散列)算法的哈希表。通过详细讲解`Simple_Hash`、`Hash`以及`HashTable`的概念,并结合`main`函数展示具体应用,帮助读者理解哈希数据结构在存储和查找效率上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Simple_Hash:

#pragma once
class Simple_Hash
{
	int* data;
	int count;
	int capacity=10000;
public:
	Simple_Hash()
	{
		data = new int[capacity]();
		count = 0;
	}

	void insert(int key, int val)
	{
		data[key] = val;
		count++;
	}

	void remove(int key)
	{
		data[key] = 0;
		count--;
	}

	int search(int key)
	{
		return data[key];
	}

	~Simple_Hash()
	{
		delete[] data;
	}
};


Hash:

#pragma once
#include <iostream>
class Hash
{
	int* data;
	int count;
	int length;
public:
	Hash(int length) :length(length)
	{
		data = new int[length]();
		count = 0;
	}
	~Hash()
	{
		delete[] data;
	}
	//插入
	bool insert(int key)
	{
		if (full())
		{
			return false;
		}
		int base = key % length;	//插入位置
		int step=0;	//偏移位置

		int i = base;
		while (data[i] != 0)
		{
			if (data[i] == key)	//相等,则插入重复
			{
				return false;
			}
			step++;
			i = (base + step) % length;	//循环判断i值
		}
		data[i] = key;	//赋值
		count++;
		return true;
	}
	//判断是否未满
	bool full()
	{
		//容器留一个空位
		return count >= length - 1;
	}
	//返回大小
	int size()
	{
		return count;
	}
	//判断是否为空
	bool empty()
	{
		return count == 0;
	}
	//搜索
	int search(int key)
	{
		int base = key % length;
		int step = 0;
		int i = base;
		while (true)
		{
			if (data[i] == 0)	//没有找到数据
			{
				return 0;
			}
			if (data[i] == key)
			{
				return data[i];
			}
			step++;
			i = (base + step) % length;
		}
	}
	//删除数据
	bool remove(int key)
	{
		if (empty())
		{
			return false;
		}
		int base = key % length;
		int step = 0;
		int i = base;

		while (data[i] != 0) {
			if (data[i] == key)	//找到删除的元素
			{
				data[i] = 0;
				auto next = (i + 1) % length;	//找到下一个元素位置
				while (data[next] != 0 && (data[next] % length == base))
				{
					std::swap(data[i], data[next]);
					i = next;
					next = (i + 1) % length;
				}
				count--;
				return true;
			}
			step++;
			i = (base + step) % length;
		}
	}
	//输出
	void out() {
		for (int i = 0; i < length; i++)
		{
			std::cout << data[i] << "\t";
		}
		std::cout << std::endl;
		for (int i = 0; i < length; i++)
		{
			std::cout << "[" << i << "]" << "\t";
		}
		std::cout << std::endl;
	}
};


HashTable:

#pragma once
class hashTable
{
	int length;
	int count;
	int depth;
	
	int** data;	//二维数组存表数据
public:
	hashTable(int length,int depth):length(length),depth(depth)
	{
		data = new int*[length];
		for (int i = 0; i < length; i++)
		{
			data[i] = new int[depth]();
		}
		count = 0;
	}
	~hashTable()
	{
		for (int i = 0; i < length; i++)
		{
			delete[] data[i];
		}
		delete[] data;
	}
	//插入
	bool insert(int key)
	{
		int i = key % length;
		for (int j = 0; j < depth; j++)
		{
			if (data[i][j] == key)	//重复数据
			{
				return false;
			}
			if (data[i][j] == 0)	//没数据
			{
				data[i][j] = key;
				count++;
				return true;
			}

		}
	}
	//搜索
	int search(int key)
	{
		int i = key % length;
		for (int j = 0; j < depth; j++)
		{
			if (data[i][j] == key)	//找到数据
			{
				return data[i][j];
			}
		}
		return 0;
	}
	//删除
	bool remove(int key)
	{
		int i = key % length;
		int j;
		bool ret = false;	//准备返回的值

		for (j = 0; j < depth; j++)
		{
			if (data[i][j] == key)	//找到值
			{
				ret = true;
				count--;
				break;
			}
			if (data[i][j] == 0)
			{
				return false;
			}
		}
		while (j + 1 < depth && data[i][j+1] != 0)
		{
			data[i][j] = data[i][j + 1];
			j++;
		}
		data[i][j] = 0;
		return ret;
	}
	//返回大小
	int size()
	{
		return count;
	}
	//判断是否为空
	bool empty()
	{
		return count == 0;
	}
	//输出
	void out()
	{
		for (int i = 0; i < length; i++)
		{
			std::cout << "[" << i << "]" << "\t";
		}
		std::cout << "\n";
		for (int i = 0; i < depth; i++)
		{
			for (int j = 0; j < length; j++)
			{
				if (data[j][i] != 0)
				{
					std::cout << data[j][i] << "\t";
				}
				else
				{
					std::cout << " \t";
				}
			}
			std::cout << "\n";
		}

	}
};


main+ELF:

#include <iostream>
#include <string>
#include "Simple_Hash.h"
#include "Hash.h"
#include "hashTable.h"
using namespace std;
void test()
{
	string word;
	Simple_Hash s;
	cin >> word;
	for (auto e : word)
	{
		int t = s.search(e);
		s.insert(e, ++t);
	}
	for (auto e : word)
	{
		cout << e << ":" << s.search(e) << endl;
	}
}

void test2()
{
	Hash h(10);
	h.insert(20);
	h.insert(33);
	h.insert(44);
	
	h.out();
	cout << boolalpha << h.insert(44) << endl;
	cout << boolalpha << h.insert(66) << endl;

	cout << h.search(20) << endl;
	cout << boolalpha << h.remove(33) << endl;
	h.out();
}

void test3()
{
	hashTable ht(10, 5);
	ht.insert(30);
	ht.insert(17);
	ht.insert(26);
	ht.insert(2);
	ht.insert(1);
	ht.insert(99);
	ht.insert(666);
	ht.insert(100000);
	ht.out();

	ht.insert(469);
	ht.insert(147);
	ht.insert(258369);
	ht.insert(253);
	ht.out();

	ht.remove(99);
	ht.remove(100000);
	ht.out();
	cout << ht.search(147) << endl;
	cout << ht.search(255) << endl;
}
int ELF(char* key,int MOD)
{
	unsigned long h = 0;
	while (*key)
	{
		h = (h << 4) + *key++;
		unsigned long g = h & 0xF0000000L;
		if (g)
			h ^= g >> 24;
		h &= ~g;
	}
	return h % MOD;
}

void test4()
{
	hashTable ht(10, 5);
	string str1 = "Alice";
	string str2 = "Bobb";
	ht.insert(ELF((char*)(str1.c_str()), 10));
	ht.insert(ELF((char*)(str2.c_str()), 10));
	ht.out();
	cout<<ht.search(ELF((char*)(str2.c_str()), 10))<<endl;
}

int main()
{
	//test();
	//test2();
	//test3();
	test4();
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值