您的位置:首页 > 大数据 > 人工智能

Data Structures (Weiss) Chapter 5: Separate Chain

2013-07-10 10:36 232 查看
//

// main.cpp

// Data Structure TRY1

//

// Created by zr9558 on 6/7/13.

// Copyright (c) 2013 zr9558. All rights reserved.

//

// Data Structure C++, Weiss, P.189 Separate Chaining

// we can not use the class HashTable directly, since there is a hash function in the class which we do not define it. Therefore, in P.190, the author gives an example about Employee.

#include<iostream>

using namespace
std;

#include<vector>

#include<list>

#include<algorithm>

bool isPrime(
int n)
{

for( int i=2; i*i<=n; ++i)

if( n%i==0)
return false;



return
true;
}

// nextPrime is used in rehash function.
int nextPrime(int n)
{

while( !isPrime(n)) ++n;



return n;
}

// 37 is a prime. The hash function is a hash function for string.
int hash(
const string &key)
{

int hashVal=0;

for( int i=0; i!=key.size(); ++i)
hashVal+=37*hashVal+key[i];



return hashVal;
}

// The hash function is a hash function for integers.
int hash(
int key)
{

return key;
}

template<typename HashedObj>
class HashTable
{

public:
HashTable(
int size=101);



bool contains( const HashedObj &x)
const;



void makeEmpty();
// clear all lists

bool insert( const HashedObj &x);

// x is already in HashTable, return false; else return true;

bool remove( const HashedObj &x);

// x is in HashTable and remove it, return true; else return false;



private:

vector<list<HashedObj> > theLists;

int currentSize;



void rehash()
{

vector<list<HashedObj> > oldLists=theLists;

// Create new double-sized, empty table;

theLists.resize(nextPrime(2*theLists.size()));

for( int j=0; j<theLists.size(); ++j)

theLists[j].clear();



//Copy table over

currentSize=0;

for( int i=0; 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);
// the hash function changes HashedObj x to an integer


hashVal%=theLists.size();

if( hashVal<0)
hashVal+=theLists.size();



return hashVal;
}
};

template<typename HashedObj>
void
HashTable<HashedObj>::makeEmpty()
{

for( int i=0; i<theLists.size(); ++i)

theLists[i].clear();
}

template<typename HashedObj>
bool
HashTable<HashedObj>::contains(
const HashedObj &x) const
{

const list<HashedObj> & whichList=
theLists[myhash(x)];

return find(whichList.begin(), whichList.end(),x)!=whichList.end();
}

template<typename HashedObj>
bool
HashTable<HashedObj>::remove(
const HashedObj &x)
{

list<HashedObj> & whichList=theLists[myhash(x)];



if( find(whichList.begin(), whichList.end(),x)==whichList.end())
return false;


whichList.erase( find(whichList.begin(), whichList.end(),x)==whichList.end());



--currentSize;

return
true;
}

template<typename HashedObj>
bool
HashTable<HashedObj>::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);



if( ++currentSize>theLists.size())

rehash();

return
true;
}

int main()
{















return
0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: