实现:

 #ifndef SEPARATE_CHAINING_H
#define SEPARATE_CHAINING_H #include <vector>
#include <list>
#include <string>
#include <algorithm>
using namespace std; int nextPrime( int n ); // SeparateChaining Hash table class
//
// CONSTRUCTION: an approximate initial size or default of 101
//
// ******************PUBLIC OPERATIONS*********************
// bool insert( x ) --> Insert x
// bool remove( x ) --> Remove x
// bool contains( x ) --> Return true if x is present
// void makeEmpty( ) --> Remove all items
// int hash( string str ) --> Global method to hash strings template <typename HashedObj>
class HashTable
{
public:
explicit HashTable( int size = )
: currentSize( )
{ theLists.resize( ); } bool contains( const HashedObj & x ) const
{
const list<HashedObj> & whichList = theLists[ myhash( x ) ];
return find( whichList.begin( ), whichList.end( ), x ) != whichList.end( );
} void makeEmpty( )
{
for( int i = ; i < theLists.size( ); i++ )
theLists[ i ].clear( );
} bool insert( const HashedObj & x )
{
list<HashedObj> & whichList = theLists[ myhash( x ) ];
if( find( whichList.begin( ), whichList.end( ), x ) != whichList.end( ) )
return false;
whichList.push_back( x ); // Rehash; see Section 5.5
if( ++currentSize > theLists.size( ) )
rehash( ); return true;
} bool remove( const HashedObj & x )
{
list<HashedObj> & whichList = theLists[ myhash( x ) ];
list<HashedObj>::iterator itr = find( whichList.begin( ), whichList.end( ), x ); if( itr == whichList.end( ) )
return false; whichList.erase( itr );
--currentSize;
return true;
} private:
vector<list<HashedObj> > theLists; // The array of Lists
int currentSize; void rehash( )
{
vector<list<HashedObj> > oldLists = theLists; // Create new double-sized, empty table
theLists.resize( nextPrime( * theLists.size( ) ) );
for( int j = ; j < theLists.size( ); j++ )
theLists[ j ].clear( ); // Copy table over
currentSize = ;
for( int i = ; i < oldLists.size( ); i++ )
{
list<HashedObj>::iterator itr = oldLists[ i ].begin( );
while( itr != oldLists[ i ].end( ) )
insert( *itr++ );
}
} int myhash( const HashedObj & x ) const
{
int hashVal = hash( x ); hashVal %= theLists.size( );
if( hashVal < )
hashVal += theLists.size( ); return hashVal;
}
}; int hash( const string & key );
int hash( int key ); #endif

测试:

 #include "SeparateChaining.h"
#include <iostream>
using namespace std; /**
* Internal method to test if a positive number is prime.
* Not an efficient algorithm.
*/
bool isPrime( int n )
{
if( n == || n == )
return true; if( n == || n % == )
return false; for( int i = ; i * i <= n; i += )
if( n % i == )
return false; return true;
} /**
* Internal method to return a prime number at least as large as n.
* Assumes n > 0.
*/
int nextPrime( int n )
{
if( n % == )
n++; for( ; !isPrime( n ); n += )
; return n;
} /**
* A hash routine for string objects.
*/
int hash( const string & key )
{
int hashVal = ; for( int i = ; i < key.length( ); i++ )
hashVal = * hashVal + key[ i ]; return hashVal;
} /**
* A hash routine for ints.
*/
int hash( int key )
{
return key;
}

数据结构-Hash表的更多相关文章

  1. php 数据结构 hash表

    hash表 定义 hash表定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值进行数据库搜索更快,这种方法一般用来 ...

  2. java数据结构之hash表

    转自:http://www.cnblogs.com/dolphin0520/archive/2012/09/28/2700000.html Hash表也称散列表,也有直接译作哈希表,Hash表是一种特 ...

  3. 【数据结构】Hash表

    [数据结构]Hash表 Hash表也叫散列表,是一种线性数据结构.在一般情况下,可以用o(1)的时间复杂度进行数据的增删改查.在Java开发语言中,HashMap的底层就是一个散列表. 1. 什么是H ...

  4. Redis原理再学习04:数据结构-哈希表hash表(dict字典)

    哈希函数简介 哈希函数(hash function),又叫散列函数,哈希算法.散列函数把数据"压缩"成摘要,有的也叫"指纹",它使数据量变小且数据格式大小也固定 ...

  5. 【数据结构】非常有用的hash表

        这篇博客的目的是让尚未学会hash表的朋友们对hash表有一个直观的理解,并且能根据本文定义出属于自己的第一个hash表,但算不上研究文,没有深究概念和成功案例.         什么是has ...

  6. 【数据结构】Hash表简介及leetcode两数之和python实现

    文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...

  7. 6.数组和Hash表

    当显示多条结果时,存储在变量中非常智能,变量类型会自动转换为一个数组. 在下面的例子中,使用GetType()可以看到$a变量已经不是我们常见的string或int类型,而是Object类型,使用-i ...

  8. PHP数组/Hash表的实现/操作、PHP变量内核实现、PHP常量内核实现 - [ PHP内核学习 ]

    catalogue . PHP Hash表 . PHP数组定义 . PHP变量实现 . PHP常量实现 1. PHP Hash表 0x1: 基本概念 哈希表在实践中使用的非常广泛,例如编译器通常会维护 ...

  9. hash-1.hash表和hash算法

    1.hash表 哈希表,也叫散列表,是根据关键码(Key)而直接访问的数据结构,也就是它把Key映射到表中一个位置来访问记录,即,把key计算成hashcode,把hashcode存到表中.这个把ke ...

随机推荐

  1. 算法_栈的Java的通用数组实现

    栈是一个常用的最简单的数据结构,这里提供了其实现.内部维护了一个数组,并且可以动态的调整数组的大小.而且,提供了迭代器支持后进先出的迭代功能.Stack的实现是所有集合类抽象数据类型实现的模板,它将所 ...

  2. Android保存图像到相册

    在应用的图集中,通常会给用户提供保存图片的功能,让用户可以将自己喜欢的图片保存到系统相册中. 这个功能其实很好做,系统提供了现成的API: 简单的来说就这一行代码: [java]  MediaStor ...

  3. JavaWeb 6 Http

    6 Http 2 Http协议入门        2.1 什么是http协议                http协议: 对浏览器客户端 和  服务器端 之间数据传输的格式规范 2.2 查看http ...

  4. [css] vertical-align和line-height

    原文链接:http://www.zhangxinxu.com/wordpress/2015/08/css-deep-understand-vertical-align-and-line-height/ ...

  5. SSM框架——以注解形式实现事务管理

    上一篇博文<SSM三大框架整合详细教程>详细说了如何整合Spring.SpringMVC和MyBatis这三大框架.但是没有说到如何配置mybatis的事务管理,在编写业务的过程中,会需要 ...

  6. HTML的超链接

    一.超链接概念: 超链接也叫 URL 中文翻译为资源定址器.这个定址器的功能主要告诉浏览器根据 URL的地址找到所需的资源.作用于连接资源 二.超链接的常用属性: 1.href=指定目的地,当有了hr ...

  7. JQuery基础汇总

    1. 对象获取与赋值::$("#obj").val("Hello World!"); 2. 对象的显示与隐藏:$("#obj").show( ...

  8. C\C++头文件说明

    C\C++编程时候经常会遇到头文件问题而出现一系列的调试错误,下面我就简要的举例介绍一下头文件的作用,我们知道一个C\C++ 程序中开头一般都为: #include<iostream.h> ...

  9. DI 之 3.3 更多DI的知识(柒)

    3.3.3  自动装配 3.3.4  依赖检查 3.3.5 方法注入

  10. robotframework笔记2

    法文件和目录 层次结构安排测试用例构建如下: 在创建测试用例 测试用例文件 . 自动创建一个测试用例文件 测试套件 包含 测试用例文件. 一个目录包含测试用例文件形成一个更高级别的测试 套件. 这样一 ...