实现:

 #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. N-Queens II

    Description: Follow up for N-Queens problem. Now, instead outputting board configurations, return th ...

  2. 闲谈--心态 (zhuan)

    http://blog.csdn.net/marksinoberg/article/details/53261034 ***************************************** ...

  3. spring相关资源

    1. 文档中英文 Spring Framework Reference Documentation http://docs.spring.io/spring/docs/4.1.7.RELEASE/sp ...

  4. JavaScript设计模式与开发实践 - 策略模式

    引言 本文摘自<JavaScript设计模式与开发实践> 在现实中,很多时候也有多种途径到达同一个目的地.比如我们要去某个地方旅游,可以根据具体的实际情况来选择出行的线路. 如果没有时间但 ...

  5. 启动eclipse说在sdk目录下的platforma-tools下面找不到adb.exe

      adb是什么?adb有什么用?adb工具如何用? 是用来管理模拟器和真机的通用调试工具,该工具功能强大,直接打开cmd即可使用adb命令,adb的全称为Android Debug Bridge,是 ...

  6. xUtils更新到3.0后的基本使用规则

    说实话,对于xUtils,是我最近才用到的开发框架(也是刚接触),对于其功能不得不说,简化了很多的开发步骤,可以说是非常好的开发工具,但是其最近更新到3.0也没有解决加载自定义ImageView报错的 ...

  7. java 导入包

    导入包 问题:类名冲突时,要如何解决. 解决:sun提供导入包语句让我们解决该问题. 导入包语句的作用:简化书写. 导入包语句的格式:import 包名.类名;(导入xxx包的XX类) 导入包语句的细 ...

  8. B2车

    晚上10点之后,杭州快速公交B2的司机,把2节车厢,开的像是跑车一样,每次启动都是弹射出去的,好像是在报复白天蜗牛般的速度.真乃是见神杀神,见佛杀佛.

  9. Eclipse全面提速小技巧

    转自:http://rongmayisheng.com/post/eclipse%E5%85%A8%E9%9D%A2%E6%8F%90%E9%80%9F 欢迎关注我的社交账号: 博客园地址: http ...

  10. Java开发Maven环境配置和介绍

    最近很火热的12306的订票软件go-home,我也下载了一份下来了,使用了一下,也从svn中把代码down下来了,但是在eclipse中竟然出错了,依赖的jar包都没有找到,后来才知道人家是用mav ...