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

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

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 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值