#include <stdio.h>
typedef struct node_t{
struct node_t *next;
char *word;
int count;
}*node;
#define NHASH 9973 // 最好定位质数
#define MULT 31 // 乘法器
node bin[NHASH]; // 哈希表索引
unsigned int hash(char *p)
{
unsigned int h = 0;
for(; *p; p++)
h = MULT * h + *p;
return h % NHASH;
}
void incword(char *s)
{
unsigned int h = hash(s);
node p;
for(p=bin[h]; p; p=p->next)
if(strcmp(s, p->word) == 0){
(p->count)++;
return;
}
p = malloc(sizeof(*p));
p->count = 1;
p->word = malloc(strlen(s)+1);
strcpy(p->word, s);
p->next = bin[h]; // 栈式插入,从表头处插入,而非从表的尾部插入
bin[h] = p;
}
void main(int argc, char **argv)
{
int i;
char buf[32];
for(i=0;
哈希表之词频统计
最新推荐文章于 2023-02-08 13:17:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)