前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 数据结构-散列表(Hash Table

原 数据结构-散列表(Hash Table

作者头像
不高不富不帅的陈政_
发布2018-05-18 15:50:39
5690
发布2018-05-18 15:50:39
举报
文章被收录于专栏:聊聊技术

No BB, just show you the code.

代码语言:javascript
复制
/**hash_chaining.h
 * The Chaining Hash Table Data Structure in C++
 * Time Cost : Search / Insert / Delete : Θ(1)
 * Thanks to Introduction to Algorithms (CLRS) Chapter 11
 * Thanks to Stanford MOOC of "Algorithms : Part I"
 * Author: Zheng Chen / Arclabs001
 * Email : chenzheng17@yahoo.com
 * Copyright 2015 Xi'an University of Posts & Telecommunications. All rights reserved.
 */
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Node     //The hash chaining node
{
    int key;
    string name;
    Node * next;
};

class hash_table
{
private:
    vector<Node *> _map;
    int _hash(int _key) {return (_key*73)%101;}  //The hash function

public:
    hash_table() {_map.reserve(105); for(int i=0;i<=100;i++) _map[i]=nullptr;}  //Constructor

    void hash_insert(int _key, string _name);
    void hash_insert(Node* New_Node);

    void hash_delete(int _key);
    void hash_delete(int _key, string _name);
    void hash_delete(Node* node);

    Node* hash_search(int _key);
    Node* hash_search(int _key, string _name);
};
/**
 * Insert a node into the hash table
 * @param _key  : The key value
 * @param _name : One of the satelitte data 
 */
void hash_table::hash_insert(int _key, string _name)
{
    Node * New_Node = new Node;
    New_Node->key = _key;
    New_Node->name = _name;
    hash_insert(New_Node);
}
/**
 * Insert a node into the hash Table
 * @param New_Node : The node to be inserted
 */
void hash_table::hash_insert(Node* New_Node)
{
    New_Node->next = _map[_hash(New_Node->key)];
    _map[_hash(New_Node->key)] = New_Node;
}
/**
 * Delete all of the node which value equals to key
 * @param _key : The key value
 */
void hash_table::hash_delete(int _key)
{
    if(_map[_hash(_key)] == nullptr)
       return;

    int num = _hash(_key);
    Node* p = _map[num];
    while(_map[num])
    {
        p = _map[num];
        _map[num] = p->next;
        delete p;
    }
}
/**
 * Delete a node which valued _key and name = _name
 * @param _key  : The key value
 * @param _name : One of the satelitte data 
 */
void hash_table::hash_delete(int _key, string _name)
{
    if(_map[_hash(_key)] == nullptr)
        return;

    int num = _hash(_key);
    Node * p = _map[num];
    Node * q = _map[num];
    while(p!=nullptr)
    {
        if(p->name == _name)
            break;
        q = p;
        p = p->next;
    }

    if(q == _map[num] && p == q)
        _map[num] = p->next;
    else
        q->next = p->next;
}
/**
 * Delete a node from the hash table which equals to node
 * @param node : The node to be deleted
 */
void hash_table::hash_delete(Node* node)
{
    hash_delete(node->key, node->name);
}
/**
 * Search the node which valued key
 * @param  _key : The key value
 * @return      : a pionter to the first node
 */
Node* hash_table::hash_search(int _key)
{
    return _map[_hash(_key)];
}
/**
 * Find a node which valued key and its name equals to _name
 * @param  _key  : The key value
 * @param  _name : One of the satelitte data 
 * @return       : A pionter to the target node
 */
Node* hash_table::hash_search(int _key, string _name)
{
    if(_map[_hash(_key)] == nullptr)
    {
        return nullptr;
    }

    Node* p = _map[_hash(_key)];
    while(p)
    {
        if(p->name == _name)
            return p;
        p = p->next;
    }
    return nullptr;
}
代码语言:javascript
复制
#include "hash_chaining.h"

int main()
{
    hash_table _hash_table;
    Node *_node = _hash_table.hash_search(3);   //此时表为空,测试search
    if(_node == nullptr)
        cout<<"Now _node is a nullptr!"<<endl;

    string name = "asdfg";

    for(int i=0; i<5; i++)  //插入节点
    {
        name[i]++;
        _hash_table.hash_insert(i,name);
    }

    _node = _hash_table.hash_search(3);     //测试search(int _key)接口
    cout<<_node->name<<endl;

    _hash_table.hash_delete(3,"btegg");     //测试delete(int _key, string _name)接口
    _node = _hash_table.hash_search(3);
    if(_node == nullptr)
        cout<<"Now _node is a nullptr!"<<endl;
    else
        cout<<_node->name<<endl;

    _hash_table.hash_insert(2,"b");     //测试insert(int _key, string _name)接口

    _node = _hash_table.hash_search(2,"btefg");     //测试search(int _key, string _name)接口
        cout<<_node->name<<endl;

    _node = _hash_table.hash_search(2,"b"); 
        cout<<_node->name<<endl;

    _hash_table.hash_delete(2,"btefg");
    _node = _hash_table.hash_search(2);
    if(_node == nullptr)
        cout<<"Now _node is a nullptr!"<<endl;
    else
        cout<<_node->name<<endl;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档