首页 > 其他 > 详细

散列表(HashTable)

时间:2016-01-06 23:13:59      阅读:308      评论:0      收藏:0      [点我收藏+]

  散列表

    i. 散列函数

    i. 冲突解决

      ii. 分离链表法

      ii. 开放地址法

        iii. 线性探测法

        iii. 平方探测法

        iii. 双散列

      ii. 再散列

      ii. 可扩散列

    i. 装填因子:元素个数 / 散列表大小

 

  C++实现

   1. 散列函数:key % tableSize 

    冲突解决:分离链表法

技术分享
/*
    散列函数: key % tableSize
    冲突解决: 分离链表法
*/
#include <iostream>
#include <vector>
#include <list>

using namespace std;

typedef int value;

class HashTable
{
public:
    HashTable(unsigned size) : _size(size) 
    {
        _table = new vector<list<value>* >(size);

        for (size_t i = 0; i < _size; i++)
        {
            (*_table)[i] = new list<value>;
        }
    }

    void insert(value val)
    {
        (*_table)[ val % _size ]->push_back(val);
    }

    value find(value val)
    {
        list<value>::const_iterator it = (*_table)[val % _size]->cbegin();

        while (it != (*_table)[val % _size]->cend())
        {
            if (*it == val)
                return *it;

            it++;
        }

        return NULL;
    }

    ~HashTable()
    {
        for (size_t i = 0; i < _size; i++)
        {
            delete (*_table)[i];
        }

        delete _table;
    }

private:
    vector<list<value>* >* _table;
    unsigned _size;
};

int main()
{
    HashTable h(11);

    h.insert(4);
    h.insert(4 + 11);

    cout << h.find(4 + 11);

    return 0;
}
View Code

 

散列表(HashTable)

原文:http://www.cnblogs.com/fengyubo/p/5107511.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!