Data Structures (Weiss) Chapter 5: Separate Chain

//

//  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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值