数据结构-Hash表
实现:
#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表的更多相关文章
- php 数据结构 hash表
hash表 定义 hash表定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值进行数据库搜索更快,这种方法一般用来 ...
- java数据结构之hash表
转自:http://www.cnblogs.com/dolphin0520/archive/2012/09/28/2700000.html Hash表也称散列表,也有直接译作哈希表,Hash表是一种特 ...
- 【数据结构】Hash表
[数据结构]Hash表 Hash表也叫散列表,是一种线性数据结构.在一般情况下,可以用o(1)的时间复杂度进行数据的增删改查.在Java开发语言中,HashMap的底层就是一个散列表. 1. 什么是H ...
- Redis原理再学习04:数据结构-哈希表hash表(dict字典)
哈希函数简介 哈希函数(hash function),又叫散列函数,哈希算法.散列函数把数据"压缩"成摘要,有的也叫"指纹",它使数据量变小且数据格式大小也固定 ...
- 【数据结构】非常有用的hash表
这篇博客的目的是让尚未学会hash表的朋友们对hash表有一个直观的理解,并且能根据本文定义出属于自己的第一个hash表,但算不上研究文,没有深究概念和成功案例. 什么是has ...
- 【数据结构】Hash表简介及leetcode两数之和python实现
文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...
- 6.数组和Hash表
当显示多条结果时,存储在变量中非常智能,变量类型会自动转换为一个数组. 在下面的例子中,使用GetType()可以看到$a变量已经不是我们常见的string或int类型,而是Object类型,使用-i ...
- PHP数组/Hash表的实现/操作、PHP变量内核实现、PHP常量内核实现 - [ PHP内核学习 ]
catalogue . PHP Hash表 . PHP数组定义 . PHP变量实现 . PHP常量实现 1. PHP Hash表 0x1: 基本概念 哈希表在实践中使用的非常广泛,例如编译器通常会维护 ...
- hash-1.hash表和hash算法
1.hash表 哈希表,也叫散列表,是根据关键码(Key)而直接访问的数据结构,也就是它把Key映射到表中一个位置来访问记录,即,把key计算成hashcode,把hashcode存到表中.这个把ke ...
随机推荐
- 国内外常用的DNS服务器
国内外常用的DNS服务器 DNS,全称Domain Name System,即域名解析系统,帮助用户在互联网上寻找路径,它在互联网的作用是把域名转换成为网络可以识别的IP地址. 国外DNS服务器地址: ...
- 这个算asp.net的一个bug吗?
asp.net设置按钮Enabled="false"后OnClientClick中添加的验证脚本消失了 下面的确可以 <asp:Button ID="btnRegi ...
- iOS——Xcode中添加第三方库
一.只有.h和.a文件的库 1.向项目中添加三方库文件 如果添加的第三方库只有.h和.a文件,直接把文件夹拖进项目下面,这时会弹出下面的提示框,一定要勾选下面选择的选项: 这里要注意,在Add to ...
- 转!!MAC和PHY的区别
一块以太网网卡包括OSI(开方系统互联)模型的两个层,物理层和数据链路层.物理层定义了数据传送与接收所需要的电与光信号.线路状态.时钟基准.数据编码和电路等,并向数据链路层设备提供标准接口.数据链路层 ...
- Request 接收参数乱码原理解析
起因: 今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: &l ...
- enum类型
1.设有变量a,b,c被说明为上述的weekday,可采用下述任一种方式: enum weekday{sun,mon,tue,wed,thu,fri,sat}; enum weekday a,b,c; ...
- shell中for循环
shell中for循环总结 最常用的就是遍历操作某一类文件,比如批量建索引. for i in `ls` do samtools faidx $i done 注意:for末尾不需要冒号(:),循环的代 ...
- 抛弃优启Grub4dos和PE大多数时间可以这样用
在能够进入Windows的情况下,Grub4dos和PE大多数时间可以这样用 http://files.cnblogs.com/files/liuzhaoyzz/boot_moban.rar Grub ...
- python2 httplib 笔记
python2 httplib 笔记 #coding=utf-8 ''' Created on 2014年9月25日 @author: cocoajin ''' import httplib,url ...
- 编译spock proxy
今天把spock proxy编译通过并且运行了.大家如果在编译这款类似于MySQL proxy的软件遇到问题时,可以联系我.微信onesoft007