Hash-table(用除法散列法实现)

Hash-table(用除法散列法实现)

关键字只支持int型,初学者版本
用链表解决冲突问题


#ifndef C11LEARN_HASHDIVISION_H
#define C11LEARN_HASHDIVISION_H
#include "Chain.h"
template<typename T>
class HashDivision
{
protected:int capacity;Chain<T>**array;
public:HashDivision(int capacity = 701);HashDivision(const HashDivision<T> &hashDivision);const HashDivision<T> & operator =(const HashDivision<T> &hashDivision);T& operator[](int key);const T operator[](int key) const;Chain<T>* search(int key);bool insert(Chain<T>* node);bool remove(Chain<T>* node);virtual ~HashDivision();protected:virtual int hashing(int index);void clear();void copy(const HashDivision<T> &hashDivision);
};
template<typename T>
HashDivision<T>::HashDivision(int capacity):capacity(capacity){array = new Chain<T>* [this->capacity];
}
template<typename T>
HashDivision<T>::~HashDivision(){clear();delete [] array;array = nullptr;
}
template<typename T>
void HashDivision<T>::clear()
{Chain<T>* current;Chain<T>* next;for (int i = 0; i < capacity; ++i) {current = array[i];while (current!= nullptr){next = current->next;delete current;current = next;}}
}
template<typename T>
HashDivision<T>::HashDivision(const HashDivision<T> &hashDivision){capacity = hashDivision.capacity;array = new Chain<T>* [capacity];copy(hashDivision);
}
template<typename T>
const HashDivision<T> & HashDivision<T>::operator =(const HashDivision<T> &hashDivision){if(this == &hashDivision) return *this;clear();delete [] array;capacity = hashDivision.capacity;array = new Chain<T>* [capacity];copy(hashDivision);return *this;
}
template<typename T>
void HashDivision<T>::copy(const HashDivision<T> &hashDivision){Chain<T>* current;Chain<T>* prev;for (int i = 0; i < capacity; ++i) {prev = nullptr;current = hashDivision.array[i];while (current!= nullptr){prev = current;current = current->next;}while (prev!= nullptr){Chain<T>* node = new Chain<T>();node->key = prev->key;node->value = prev->value;insert(node);prev = prev->prev;}}
}
template<typename T>
T& HashDivision<T>::operator[](int key){Chain<T>* node = search(key);if(node == nullptr){node = new Chain<T>();node->key = key;insert(node);}return node->value;
}
template<typename T>
const T HashDivision<T>::operator[](int key) const{Chain<T>* node = search(key);if(node == nullptr){throw "no find this key";}return node->value;
}
template<typename T>
Chain<T>* HashDivision<T>::search(int key)
{int hash_code = hashing(key);Chain<T>* current = array[hash_code];while (current!= nullptr && current->key != key){current =current->next;}return current;
}
template<typename T>
int HashDivision<T>::hashing(int index){return index % capacity;
}
template<typename T>
bool HashDivision<T>::insert(Chain<T>* node){if(node == nullptr) return false;int hash_code = hashing(node->key);Chain<T>* current = array[hash_code];if(current == nullptr){array[hash_code] = node;}else{node->next = current;current->prev = node;array[hash_code] = node;}return true;
}
template<typename T>
bool HashDivision<T>::remove(Chain<T>* node){if(node == nullptr) return false;if(node->prev == nullptr){int hash_code = hashing(node->key);array[hash_code] = node->next;}else{node->prev->next = node->next;if(node->next!= nullptr)node->next->prev = node->prev;}delete node;node = nullptr;return true;
}#endif //C11LEARN_HASHDIVISION_H

辅助类
Chain代码链接

测试代码

    HashDivision<string> hashDivision;hashDivision[2] = "hello";hashDivision[5] = "world";cout<<hashDivision[2]<<endl;cout<<hashDivision[5]<<endl;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/308967.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作

前段时间在做用户画像的时候&#xff0c;遇到了这样的一个问题&#xff0c;记录某一个商品的用户购买群&#xff0c;刚好这种需求就可以用到Redis中的Set&#xff0c;key作为productID&#xff0c;value就是具体的customerid集合&#xff0c;后续的话&#xff0c;我就可以通过p…

IDEA如何在包下建立子包

idea如何在包下建立子包 第一次在包下建立子包时候出现了问题 在java > springmvc包下再new上一个package controller的时候就会出现这个样子 如何解决 在IDEA2019 中的Show Options Menu下有一个Compacket Middle Packages将它关闭即可 解决成功

辅助类Chain

辅助类Chain #ifndef C11LEARN_CHAIN_H #define C11LEARN_CHAIN_H template<typename T> class Chain { public:int key;T value;Chain<T> *prev;Chain<T> *next; public:Chain(){}Chain(int key,const T value):key(key),value(value){} }; #endif //C11LEA…

.NET Core微服务开发选项

微服务开发的关注点有哪些&#xff1f;微服务构最终的目标是实现业务的价值&#xff0c;交付&#xff0c;为了让开发人员更加关注业务开发和交付&#xff0c;微服务需要一些比较底层的基础设置&#xff0c;我们也称为微服务公共关注点。配置管理&#xff1a;对微服务可变参数进…

常见的HTTP状态码(HTTP Status Code)说明

作为一个互联网开发人员对于一些服务器返回的HTTP状态的意思都必须是了如指掌的&#xff0c;只有将这些状态码一一弄清楚&#xff0c;工作中遇到的各种问题才能够处理的得心应手。好了&#xff0c;下面就让我们来了解一下比较常见的HTTP状态码吧&#xff01; 2开头 &#xff0…

.NET 开源项目 StreamJsonRpc 介绍[上篇]

StreamJsonRpc 是一个实现了 JSON-RPC 通信协议的开源 .NET 库&#xff0c;在介绍 StreamJsonRpc 之前&#xff0c;我们先来了解一下 JSON-RPC。JSON-RPC 介绍JSON-RPC 是一个无状态且轻量级的远程过程调用&#xff08;RPC&#xff09;协议&#xff0c;其使用 JSON&#xff08;…

hash table(用乘法散列法实现)

hash table(用乘法散列法实现&#xff09; #ifndef C11LEARN_HASHMULTI_H #define C11LEARN_HASHMULTI_H #include "HashDivision.h" template<typename T> class HashMulti:public HashDivision<T> { private:long w;long p;long long s;long long two_…

gRPC真要取代WebApi了,你还学得过来吗?

今年1月份微软曾宣布要实验性的对.NET支持 gRPC-Web&#xff0c;然后在6月份已经正式发布了。这些天尝试了下&#xff0c;真的很强大&#xff0c;不负责任的预言下&#xff0c;RESTful的时代即将过去&#xff0c;而gRPC要成为革命者&#xff01;先别急眼&#xff0c;下面我来详…

hash table(开放寻址法-线性探查实现的哈希表)

hash table(开放寻址法-线性探查实现的哈希表&#xff09; // // Created by 许加权 on 2021/7/17. //#ifndef C11LEARN_HASHLINER_H #define C11LEARN_HASHLINER_H #include "KeyNode.h" template<typename T> class HashLiner { public:HashLiner();HashLin…

Spring5 jar包下载

下载地址 https://repo.spring.io/simple/libs-release-local/org/springframework/spring/ Spring5最新版本的下载 选择最新版本5.2.3 下载前两项&#xff0c;解压放入文件夹中 项目中导包 ps&#xff1a;我使用的开发工具是idea 第一步&#xff1a;在file中选择project st…

hash table(开放寻址法-二次探查实现的哈希表)

hash table(开放寻址法-二次探查实现的哈希表&#xff09; #ifndef C11LEARN_HASHQUADRATIC_H #define C11LEARN_HASHQUADRATIC_H #include "HashLiner.h" template<typename T> class HashQuadratic:public HashLiner<T> { public:HashQuadratic(int c1…

优化 Azure 成本,实现财务目标

点击上方蓝字关注“汪宇杰博客”原文&#xff1a;Omar Khan General Manager, Microsoft Azure翻译&#xff1a;汪宇杰导语我们的许多客户都面临着如何满足关键 IT 项目的资金需求的困难决策。我们在此共同帮助您实现财务目标。确保 Azure 工作负载的成本得到优化有助于释放资金…

集合的定义与并查操作(C语言)

代码如下&#xff1a; #define MAXN 1000 /* 集合最大元素个数 */ typedef int ElementType; /* 默认元素可以用非负整数表示 */ typedef int SetName; /* 默认用根结点的下标作为集合名称 */ typedef ElementType SetType[MAXN]; /…

采用config方式灵活配置我们的Quarz.net中的Job,Trigger

经常在项目中遇到定时任务的时候&#xff0c;通常第一个想到的是Timer定时器&#xff0c;但是这玩意功能太弱鸡&#xff0c;实际上通常采用的是专业化的第三方调度框架&#xff0c;比如说Quartz&#xff0c;它具有功能强大和应用的灵活性&#xff0c;我想使用过的人都非常了解&…

hash table(开放寻址法-双重散列实现的哈希表)

hash table(开放寻址法-双重散列实现的哈希表&#xff09; #ifndef C11LEARN_HASHDOUBLE_H #define C11LEARN_HASHDOUBLE_H #include "HashLiner.h" template<typename T> class HashDouble:public HashLiner<T>{ protected:virtual int hashing(int key…

对于任给的一张无向带权连通图,求出其最小生成树(C++)

对于任给的一张无向带权连通图&#xff0c;求出其最小生成树。 题目要求: (1)编程创建一幅图 (2)输出创建的图 (3)编写Prim算法代码&#xff0c;实现图的最小生成树求解&#xff0c;且输出最小生成树 (4)编写Kruskal算法代码&#xff0c;实现图的最小生成树求解&#xff0c;且…

使用.Net Core实现的一个图形验证码

SimpleCaptcha是一个使用简单&#xff0c;基于.Net Standard 2.0的图形验证码模块。它的灵感来源于Edi.Wang的这篇文章https://edi.wang/post/2018/10/13/generate-captcha-code-aspnet-core&#xff0c;我将其中生成验证码的代码抽取出来进行封装得到了这个模块。下面介绍一下…

hash table(全域散列法实现的哈希表)

hash table(全域散列法实现的哈希表&#xff09; 利用每次重建哈希表时随机生成散列函数 #ifndef C11LEARN_HASHUNIVERSAL_H #define C11LEARN_HASHUNIVERSAL_H #include "Chain.h" #include "../tools/random.h" template<typename T> class HashU…

Maven编译项目时报错:不再支持源选项 5。请使用 6 或更高版本。 不再支持目标选项 1.5。请使用 1.6 或更高版本。

在使用Maven编译项目时报错&#xff1a; 不再支持源选项 5。请使用 6 或更高版本。 不再支持目标选项 1.5。请使用 1.6 或更高版本。 在项目pom.xml文件中增加maven编译的jdk版本设置&#xff0c;maven.compiler.source和maven.compiler.target&#xff1a; <properties&…