hashtable实现(未编译)

#include <iostream>
using namespace std;

template<typename Value_type>
    struct tagEntry{
    string _key;
    Value_type _value;
    struct tagEntry<Value_type>* _pNext;
    tagEntry(string k, Value_type v):_key(k), _value(v), _pNext(NULL){}

};

template<typename Value_type>
    class Hashtable {
    typedef struct tagEntry<Value_type> Entry;
    public:
    Hashtable(){
        _capacity = 31;
        _size = 0;
        _ptable = new *Entry[_capacity];    
    }
    ~Hashtable(){
        _size = 0;
        for (int i = 0; i < _capacity; i++) {
            Entry* cur = _ptable[i];
            while(cur) {
                Entry* tmp = cur;
                cur = cur->_pNext;
                delete cur;
            }
        }
        _capacity = 0;
        delete[] _ptable;
    }
    size_t size() {return _size;}
    void insert(string& key, Value_type& value) {
        _Expand();
        size_t index = hasher(key) % _capacity;
        Entry* pEntry = _ptable[index];
        if (!pEntry)
            pEntry = new Entry(key, value);
        else {
            while (pEntry->_pNext) 
                pEntry = pEntry->_pNext;
            pEntry->_pNext = new Entry(key, value);
        }
        _size++;
        return;
    }
    Value_Type& find(const string& key) {
        size_t = index hasher(key) & _capacity;
        Entry* pEntry = _ptable[index];
        while (pEntry) {
            if (pEntry->_key != key)
                pEntry = pEntry->_pNext;
            else
                return pEntry->_value;
        }
        return;
    }
    private:
    Entry** _ptable;
    size_t _capacity;
    size_t _size;
    void insert(Entry* node) {
        size_t index = haser(cur->key) & _capacity;
        Entry* pEntry = _ptable[index];
        if (!pEntry)
            pEntry = node;
        else {
            while (pEntry->_pNext)
                pEntry = pEntry->_pNext;
            pEntry->_pNext = node;
        }
    }
    void _Expand() {
        if (_size < _capacity)
            return;
        size_t new_capacity = _capacity * 2;
        Entry* pold_table = _ptable;
        _ptable = new *Entry[_capacity];
        for (int i = 0; i < _capacity; i++) {
            Entry *cur = pold_table[i];
            while(cur) {
                insert(cur);
                cur = cur->_pNext;
            }
            return;

        }
        size_t hasher(string& s) {
            register size_t h = 0;
            for(string::iterator iter = s.begin(); iter != s.end(); iter++)
                h = h*31 + *iter;
            return h;
        }    
    };

int main() {
    // your code goes here
    Hashtable<int> table;

    table.insert("abc",1);
    table.insert("bcd",2);

    cout<<table.find("abc")<<" "<<table.find("bcd")<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值