哈希表

来源:互联网 发布:dd是什么意思网络语言 编辑:程序博客网 时间:2024/06/15 19:13

欢迎大家访问我的微博:http://weibo.com/u/2887401030

我们都知道,对于数组,查询容易,插入和删除较难。而对于链表,插入和删除容易,查询较难(线性查询)。那么有没有一种数据结构,插入,删除,查询都较为容易的呢?当然有,他就是这篇文章的主角——哈希表(散列表)。

直接看代码:

#include<iostream> #include<cstdlib>using namespace std;const int HASH_SIZE = 10;typedef struct node_hash{    int key;    int count;    struct node_hash *next;}HashNode;HashNode* hashTable[HASH_SIZE];int hash_function(int key){    return key % HASH_SIZE;//return index}void init_hash(){    for(int i = 0;i < HASH_SIZE;i++)    {        hashTable[i] = NULL;    }}HashNode* query_hash(int key){    int index = hash_function(key);    HashNode *p = hashTable[index];    if(p == NULL)        return NULL;    while(p != NULL)    {        if(p->key == key)        {            p->count++;            return p;        }        p = p->next;    }    return p;}bool insert_hash(int key){    if(query_hash(key) != NULL)        return false;    int index = hash_function(key);    HashNode *node = new HashNode;    if(node == NULL)        return false;    node->key = key;    node->count = 1;    node->next = NULL;    if(hashTable[index] == NULL)    {        //no conflict        //insert directly        hashTable[index] = node;    }    else    {        //conflict exists        HashNode *p = hashTable[index];        while(p->next != NULL)        {            p = p->next;        }        p->next = node;    }    return true;}bool delete_hash(int key){    if(query_hash(key) == NULL)        return false;    int index = hash_function(key);    HashNode *p = hashTable[index];    while(p->next != NULL)    {        if(p->next->key == key)        {            HashNode *tmp = p->next;            p = p->next->next;            delete tmp;            return true;        }        p = p->next;    }    return false;}void destroy_hash(){    for(int i=0;i<HASH_SIZE;i++)    {        HashNode *p = hashTable[i];        while(p)        {            HashNode *tmp = p->next;            delete p;            p = tmp;        }    }}int main(){    init_hash();    int num[] = {12,23,56,34,14,67,56,12,23,13,23,45,10,100,110,120,230};    int len = sizeof(num)/sizeof(*num);    for(int i=0;i<len;i++)    {        if(!insert_hash(num[i]))            cout<<"insert "<<num[i]<<" error"<<endl;    }    HashNode *node = NULL;    node = query_hash(12);    node = query_hash(12);    node = query_hash(12);    if(node)    {    //  注意上面的insert也会使count++,相当于query一次,        cout<<node->key<<":"<<node->count<<endl;    }    cout<<"删除14之前的查询结果:"<<endl;    cout<<(query_hash(14) ? "exist 14" : "not exist 14")<<endl;    cout<<"删除14之后的查询结果:"<<endl;    delete_hash(14);    cout<<(query_hash(14) ? "exist 14" : "not exist 14")<<endl;    destroy_hash();    return 0;}

结果:
insert 56 error
insert 12 error
insert 23 error
insert 23 error
12:5
删除14之前的查询结果:
exist 14
删除14之后的查询结果:
not exist 14

值得注意的是,就像我在注释中写的,每次insert一次,就是相当于一次query,count++。刚开始的数组进行了两次insert12的操作,再加上之后故意的query_hash(12)三次,一共5次,与结果一致。

另外,这里的散列函数构造采用的是除数留余法。解决冲突的方法是链地址法。大家可以试试其他的解决冲突的方法,比如开放定址法,二次探测法等等。

0 0