C语言,用hash算法来实现英文字典

wangshumin2008 2009-04-24 02:45:47
下面是我写的代码,用的是线性探测在散列算法来解决冲突,来简单实现英文单词字典。定义了两个结构体数组,wordlist[]用来存放单词,hashtable[]用来存放意思,然后打印hashtable[]里的值,但是运行后,只输出主函数的第一个输出语句,然后就停了。

#include<stdio.h>
#define HASH_LEN 4000000
#define WORD_NO 10
#define M 30
#define FLAG -1

/*-----------------------*预定义两个结构体*-------------------------*/
typedef struct word
{
char *wordname;
int key;
}list;
list wordlist[WORD_NO];

typedef struct hash
{
char *wordmean;
int key;


}hashlist;
hashlist hashtable[HASH_LEN];

/*------------------*初始化wordlist[WORD_NO].wordname*----------------*/
initword()
{
static char *p = NULL;
static int i = 0;
static int j = 0;
static int sum = 0;
wordlist[0].wordname = "abc";
wordlist[1].wordname = "cba";
wordlist[2].wordname = "computer";
wordlist[3].wordname = "love";
wordlist[4].wordname = "thank";
wordlist[5].wordname = "you";
wordlist[6].wordname = "me";
wordlist[7].wordname = "soft";
wordlist[8].wordname = "stone";
wordlist[9].wordname = "xin";
for(i =0; i < WORD_NO; i++ )
{
p = wordlist[i].wordname;
for(j = 0;*(p+i)!= '\0'; j++)
{
sum = sum + *(p+i);
}
wordlist[i].key = sum;
}
}
/*------------------*初始化hashtable[HASH_LEN]*----------------*/
inithashtable()
{
static int i = 0;
static int j = 0;
for(i = 0;i < HASH_LEN; i++)
{
hashtable[i].key = FLAG;

hashtable[i].wordmean = "";

}
}

/*----------------------*哈希函数*--------------------------------*/
int h(int k)
{
k = (k+1) % M;
return k;
}

/*---------------------*计算ASCII码值并将单词意思付给hashtable[]*---------------------------*/
askl()
{
int i = 0;
int addr = 0;
int d = 0;

for(i = 0; i < WORD_NO; i++)
{

addr = (wordlist[i].key & 0x7FFFFFFF)% M;
d = addr;
if(hashtable[addr].key == FLAG)
{
hashtable[addr].key = wordlist[i].key;
hashtable[addr].wordmean = wordlist[i].wordname;

}
else
{
do
{
addr = h(addr);
if(addr == d)
{
printf("man le \n");
break;

}

}while(hashtable[addr].key!=FLAG) ;

hashtable[d].key = wordlist[i].key;
hashtable[d].wordmean = wordlist[i].wordname;

}
}
for(i = 0; i< HASH_LEN; i++)
{
printf("%d\n",hashtable[i].key);
printf("%d\n",hashtable[i].wordmean);

}

}


main()
{
printf("++++++++++++++++++++++++++++++++\n");
initword();
printf("--------------------------------\n");
inithashtable();
printf("********************************\n");
askl();
printf("*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
}

请大家看一下,如果有实现的把代码发一发啊,来学习一下。谢谢咯!
...全文
1098 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuewuzhijing11 2009-04-27
  • 打赏
  • 举报
回复
确实挺不错,只是我有那个实物啊!
看来我学习的路还长着啂。加油!
liliangbao 2009-04-27
  • 打赏
  • 举报
回复
帮顶~
wangshumin2008 2009-04-27
  • 打赏
  • 举报
回复
看了大家的建议,已经可以跑了,我是想实现字典的功能,就是用HASH表来实现输入单词后,输出单词意思。我发的是一部分代码。谢谢大家给我的建议。我再好好想想怎么实现。
wangshumin2008 2009-04-27
  • 打赏
  • 举报
回复
这个是我写的代码,就是实现字典查询功能的。 已经实现了!!! 呵呵,发在这里,希望能帮到有这种需要的朋友! 以便我们能够互相学习!


#include<stdio.h>
#define HASH_LEN 60
#define WORD_NO 10
#define M 60
#define FLAG -1

/*-----------------------*预定义两个结构体*-------------------------*/
typedef struct word
{
char *wordname;
int key;
}list;
list wordlist[WORD_NO];

typedef struct hash
{
char *wordname;
char wordmean[40];
int key;


}hashlist;
hashlist hashtable[HASH_LEN];

/*------------------*初始化wordlist[WORD_NO].wordname*----------------*/
initword()
{
static char *p = NULL;
static int i = 0;
static int j = 0;
int sum = 0;
wordlist[0].wordname = "you";
wordlist[1].wordname = "cba";
wordlist[2].wordname = "computer";
wordlist[3].wordname = "love";
wordlist[4].wordname = "thank";
wordlist[5].wordname = "abc";
wordlist[6].wordname = "me";
wordlist[7].wordname = "soft";
wordlist[8].wordname = "stone";
wordlist[9].wordname = "xin";
for(i =0; i < WORD_NO; i++ )
{
sum = 0;
p = wordlist[i].wordname;
while (*p)
{
sum = sum + (*p);
p++;
}
wordlist[i].key = sum;
}
for (i = 0;i <WORD_NO; i++)
{
printf("wordlist[%d].name= %s\n",i,wordlist[i].wordname);
printf("%d\n",wordlist[i].key);
}
}

/*------------------*初始化hashtable[HASH_LEN]*----------------*/
inithashtable()
{
static int i = 0;
static int j = 0;
for(i = 0;i < HASH_LEN; i++)
{
hashtable[i].key = FLAG;
hashtable[i].wordname = NULL;
strcpy(hashtable[i].wordmean, "this is: ");

}
}

/*----------------------*哈希函数*--------------------------------*/
int h(int k)
{
k = (k+1) % M;
return k;
}

/*---------------------*计算ASCII码值并将单词意思付给hashtable[]*---------------------------*/
askl()
{
int i = 0;
int addr = 0;
int d = 0;

for(i = 0; i < WORD_NO; i++)
{

addr = (wordlist[i].key )% M;
d = addr;
if(hashtable[addr].key == FLAG)
{
hashtable[addr].key = wordlist[i].key;
hashtable[addr].wordname = wordlist[i].wordname;
strcat(hashtable[addr].wordmean, wordlist[i].wordname);

}
else
{
do
{
addr = h(addr);
if(addr == d)
{
printf("man le \n");
break;

}

}while(hashtable[addr].key!=FLAG) ;

hashtable[addr].key = wordlist[i].key;
hashtable[addr].wordname = wordlist[i].wordname;
strcat(hashtable[addr].wordmean , wordlist[i].wordname);

}
}
for(i = 0; i< HASH_LEN; i++)
{
printf("%d\n",hashtable[i].key);
printf("%s",hashtable[i].wordname);
printf("%s\n",hashtable[i].wordmean);

}

}

/*-----------------------------------查找函数------------------------------------*/

int findword(char *p)
{

static int i = 0;
static int mod = 0;
static int d = 0;
static int sum = 0;
static char *o;
o = p;
while(*p)
{
sum = sum + (*p);
p++;
}
printf("+++++++++++++++++++++\n");
printf("%d",sum );
mod = sum % M;
d = mod;
while(hashtable[mod].key != FLAG)
{

if(strcmp(o,hashtable[mod].wordname) == 0)
{

printf("the word ");
printf("%s",o);
printf("is : %s\n",hashtable[mod].wordmean);
return 0;
}

mod = h(mod);
if(d == mod)
{
printf("bu cun zai!");
break;
}


}
return 1;
}

main()
{
static char q[10];
initword();
inithashtable();
askl();
printf("please input the word\n");
scanf("%s",q);
findword(q);

}
沐浴-vip 2009-04-24
  • 打赏
  • 举报
回复
initword() 函数有问题!
沐浴-vip 2009-04-24
  • 打赏
  • 举报
回复
++++++++++++++++++++++++++++++++
这就是调试结果!lz想实现什么?
smillyz 2009-04-24
  • 打赏
  • 举报
回复
#define HASH_LEN 4000000这么大不觉的很不合理吗?有些地方注释的让人不明白,有些地方没注释,具体的别人就不知道你要实现些什么,还有你的函数前面就没有返回值的类型,编译的时候有警告也是要注意的!!!!!
  • 打赏
  • 举报
回复
求余的hash表,要不要?
fengxiquan 2009-04-24
  • 打赏
  • 举报
回复
up

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧