c/c++语言开发共享C语言写一个散列表

目录一、快速理解散列表二、散列函数三、防撞一、快速理解散列表散列表,就是下标可以为字母的数组。假设现有一个数组int a[100],想查找其中第40个元素,则直接输入a[40]就可以了,时间复杂度为o

目录
  • 一、快速理解散列表
  • 二、散列函数
  • 三、防撞

一、快速理解散列表

散列表,就是下标可以为字母的数组。

假设现有一个数组int a[100],想查找其中第40个元素,则直接输入a[40]就可以了,时间复杂度为o ( 1 ) o(1)o(1)。

问题在于,当下标不是数字,而是一个字符串的时候,可能需要一个超大的空间才能将所有下标妥善地存放在特定的位置。例如,若以大小写字母作为下标索引,那么一位就需要预留52个空间,10位就需要52的10次方 这么大的空间,根本没有设备可以满足。

好在,52的10次方这么庞大的数字也超出了正常人的使用范围,无论多长的索引,我们能用上的值也绝对是有限的。

例如,现有下面三个字符串作为下标

key1 = "microcold";  key2 = "tinycold";  key3 = "microcool";

其实只需要选取头、尾两个字母,就能很好地区分这三个字符串,即

def hash(key):      return key[0]+key[-1]

但这种算法对索引字符的要求非常高,至少头尾不能重复。所以,现在需要能把超长字符串映射成特定短字符串而且尽量避免重复的算法。

二、散列函数

最简单的散列函数就是求余,将输入字符串按位转为整数之后求余。由于在字符串可能会转成非常大的整数,故需了解余数的性质

C语言写一个散列表

(a+b)%c=(a%c+b %c)% c    

相应地有:

(a*b)%c=((a%c)*(b %c))% c    

用c语言实现如下:

#include <stdio.h>  #define maxhash 100    //快速取幂法,a*b^n%c  int  powermod (int a, int b, int n, int c)   {        int  ans = 1;       b = b % c;       while (n > 0) {            if(n % 2 == 1)               ans = (ans * b) % c;           n = n / 2;       //b >>= 1;          b = (b * b) % c;       }       return (a*ans)%c;   }     int hash(char* key, int n){      int addr = 0;      for(int i = 0; i < n; i++){          addr += powermod(key[i], 128, i, maxhash);      }      return addr%maxhash;  }    int main(){      char* str;      int i;      while(1){          gets(str);          i = 0;          while(str[i++]!=''){}          printf("%dn",hash(str,i));      }      return 0;  }

测试如下:

>gcc hash.c  >a.exe  asdf  21  microcold  81  tinycold  12  microcool  5  minicool  81  minicold  73

三、防撞

尽管minicool和microcold撞车了,但通过100以内的位数,去表示52的9次方 的样本,也算是不错的表现了。

为了不发生撞车,则需更改数组中的元素类型——至少得是个结构体。而防止撞车的方法很简单,如果发生撞车,那我就不散列了,直接发配到一个指定的数组中。

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #define maxhash 100  typedef struct hashnode{      char *key;      int next;  } *hashnode;    struct hashnode* hashtable[maxhash];  struct hashnode* crashtable[maxhash];     //存储撞击之后的值  int numcrash=0;                   //已有的撞击值    void inittable(){      for(int i=0; i < maxhash; i++){          hashtable[i] = (hashnode)malloc(sizeof(struct hashnode));          hashtable[i]->key = null;          hashtable[i]->next = -1;          crashtable[i] = (hashnode)malloc(sizeof(struct hashnode));          crashtable[i]->key = null;          hashtable[i]->next = -1;      }  }    void insertcrash(char* str, int index, int n){      if(index == numcrash){          crashtable[numcrash]->key = (char*)malloc(sizeof(char)*n);          strcpy(crashtable[numcrash++]->key, str);  //此时新增一个节点      }      else {          if(crashtable[index]->next==-1)              crashtable[index]->next = numcrash;          insertcrash(str, hashtable[index]->next, n);      }  }    //n为字符串长度  void inserthash(char* str, int index,int n){      if(hashtable[index]->key==null){          hashtable[index]->key = (char*)malloc(sizeof(char)*n);          strcpy(hashtable[index]->key, str);      }else{          if(hashtable[index]->next==-1)              hashtable[index]->next = numcrash;          insertcrash(str, hashtable[index]->next, n);      }  }    void printhash(){      for(int i = 0; i < maxhash; i++){          if(hashtable[i]->key!=null)              printf("hashtable[%d]:%sn",i,hashtable[i]->key);          if(crashtable[i]->key!=null)              printf("crashtable[%d]:%sn",i,crashtable[i]->key);      }  }    int  powermod (int a, int b, int n, int c)   {        int  ans = 1;       b = b % c;       while (n > 0) {            if(n % 2 == 1)               ans = (ans * b) % c;           n = n / 2;       //b >>= 1;          b = (b * b) % c;       }       return (a*ans)%c;   }     int hash(char* key, int n){      int addr = 0;      for(int i = 0; i < n; i++){          addr += powermod(key[i], 128, i, maxhash);      }      return addr%maxhash;  }    int main(){      inittable();      char* str;      int i;      while(1){          gets(str);          if(strcmp(str,"exit")==0) break;          i = 0;          while(str[i++]!=''){}          inserthash(str,hash(str,i),i);          printf("%dn",hash(str,i));      }      printhash();      return 0;  }

最后得到:

>gcc hash.c  >a.exe  asdf  21  hellworld  84  microcold  81  minicool  81  tinycool  20  tinycold  12  weixiaoleng  11  exit  crashtable[0]:minicool  hashtable[11]:weixiaoleng  hashtable[12]:tinycold  hashtable[20]:tinycool  hashtable[21]:asdf  hashtable[81]:microcold  hashtable[84]:hellworld

可见一方面的确散列了,另一方面也的确防撞了。

到此这篇关于c语言写一个散列表的文章就介绍到这了,更多相关c语言写散列表内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C语言写一个散列表,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1038408.html

(0)
上一篇 2022年1月26日
下一篇 2022年1月26日

精彩推荐