哈希表 [英] hash table

查看:60
本文介绍了哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想写一个简单的哈希表:


struct Employee

{

char name [30];

int id;

char department [10];

浮动薪水;

} ;


struct Employee * hash_array [MAX_SIZE];


hash_array [n] =(struct Employee *)malloc(sizeof(Employee)) ;

strcpy(* hash_array [n] .name," John Smith");

* hash_array [n] .id = 1234;

* hash_array [n] .department =" Marketing" ;;

* hash_array [n] .salary = 4000;

....


hash_array将包含指向Employee的每个对象的所有指针

类型。


我的问题是:


如何确定上面的MAX_SIZE?

如何计算索引n来定位合适的数组项?我读了

某处素数可用于此目的(构建哈希函数)。

你能告诉我这是怎么回事吗?


谢谢!

Here''s what I am trying to write a simple hash table:

struct Employee
{
char name[30];
int id;
char department[10];
float salary;
};

struct Employee *hash_array[MAX_SIZE];

hash_array[n] = (struct Employee*)malloc(sizeof(Employee));
strcpy(*hash_array[n].name, "John Smith");
*hash_array[n].id = 1234;
*hash_array[n].department = "Marketing";
*hash_array[n].salary = 4000;
....

hash_array will contain all the pointers pointing to each object of Employee
type.

My questions are:

How to determine MAX_SIZE above?
How to calculate index, n, to locate an appropriate array item? I read
somewhere prime number can be used for such purpose(build a hash function).
Could you show me how this work?

Thanks!

推荐答案

如果您正在尝试构建一个简单的哈希表,为什么不使用mod? (%)


Leo Custodio
al ***** ****@hotmail.com


-

-----开始PGP公共密钥块-----

版本:2.6.2

mQCNAz / nyswAAAEEAM1Jl14YqNlrUGmr4vh5OKGbDg5qiFnY / Ioqa5j5j9jlTsiH

7EJNlhIvu5OV223D0REUmWbFaKBQlnZAaDRRROb52YPuZ8NQfy u / C5zvTz8qubEx

jWn + nYryqKZxQsDwjntkNIMxx5n + QB7WhDltenCFE / VxYhsTa59EWq​​Uqkz / RAAUR

tC5MZW9uYXJkbyBDLiBDdXN0b2RpbyA8YWxpZW5zcHJpdGVAaG 90bWFpbC5jb20 +

= xAh5

----- END PGP PUBLIC KEY BLOCK ---- -

" al" <人*** @ 168.net> escreveu na mensagem

新闻:R1 *********************** @ bgtnsc04-news.ops.worldnet.att.net。 ..
If you are trying to build a simple hash table, why not use mod? (%)

Leo Custodio
al*********@hotmail.com

--
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.6.2

mQCNAz/nyswAAAEEAM1Jl14YqNlrUGmr4vh5OKGbDg5qiFnY/Ioqa5j5j9jlTsiH
7EJNlhIvu5OV223D0REUmWbFaKBQlnZAaDRRROb52YPuZ8NQfy u/C5zvTz8qubEx
jWn+nYryqKZxQsDwjntkNIMxx5n+QB7WhDltenCFE/VxYhsTa59EWqUqkz/RAAUR
tC5MZW9uYXJkbyBDLiBDdXN0b2RpbyA8YWxpZW5zcHJpdGVAaG 90bWFpbC5jb20+
=xAh5
-----END PGP PUBLIC KEY BLOCK-----
"al" <al***@168.net> escreveu na mensagem
news:R1***********************@bgtnsc04-news.ops.worldnet.att.net...
这就是我想写一个简单的哈希表:

struct Employee
{char name [30];
int id;
char department [10];
浮动薪水;
};

struct Employee * hash_array [MAX_SIZE];

hash_array [n] =(struct Employee *)malloc(sizeof(Employee));
strcpy(* hash_array [n] .name," John Smith");
* hash_array [ n] .id = 1234;
* hash_array [n] .department =" Marketing" ;;
* hash_array [n] .salary = 4000;
...

hash_array将包含指向
Employee类型的每个对象的所有指针。

我的问题是:

如何确定上面的MAX_SIZE?
如何计算索引n来定位合适的数组项?我读了
某处素数可用于此目的(构建哈希
函数)。你能告诉我这是怎么回事吗?

谢谢!
Here''s what I am trying to write a simple hash table:

struct Employee
{
char name[30];
int id;
char department[10];
float salary;
};

struct Employee *hash_array[MAX_SIZE];

hash_array[n] = (struct Employee*)malloc(sizeof(Employee));
strcpy(*hash_array[n].name, "John Smith");
*hash_array[n].id = 1234;
*hash_array[n].department = "Marketing";
*hash_array[n].salary = 4000;
...

hash_array will contain all the pointers pointing to each object of Employee type.

My questions are:

How to determine MAX_SIZE above?
How to calculate index, n, to locate an appropriate array item? I read
somewhere prime number can be used for such purpose(build a hash function). Could you show me how this work?

Thanks!



" al" <人*** @ 168.net>在消息中写道

新闻:R1 *********************** @ bgtnsc04-news.ops.worldnet.att.net。 ..
"al" <al***@168.net> wrote in message
news:R1***********************@bgtnsc04-news.ops.worldnet.att.net...
这就是我想写一个简单的哈希表:

struct Employee
{char name [30];
int id;
char department [10];
浮动薪水;
};

struct Employee * hash_array [MAX_SIZE];

hash_array [n] =(struct Employee *)malloc(sizeof(Employee));


不要从malloc()转换回报。如果你忘了#include< stdlib.h>,它实际上没有给你买任何东西,实际上可以隐藏

严重的错误。此外,代码中无用的演员阵容越少,阅读和维护就越容易。

strcpy(* hash_array [n] .name," John Smith") ;
* hash_array [n] .id = 1234;
* hash_array [n] .department =" Marketing";
* hash_array [n] .salary = 4000;
......

hash_array将包含指向
员工类型的每个对象的所有指针。

我的问题是:

如何确定上面的MAX_SIZE?


这取决于你的散列函数。你如何计算散列值?

将会有多少可能的值?这将是你的MAX_SIZE。

如何计算索引n来定位合适的数组项?我读了
某处素数可用于此目的(构建哈希
函数)。你能告诉我这是怎么回事吗?
Here''s what I am trying to write a simple hash table:

struct Employee
{
char name[30];
int id;
char department[10];
float salary;
};

struct Employee *hash_array[MAX_SIZE];

hash_array[n] = (struct Employee*)malloc(sizeof(Employee));
Do not cast return from malloc(). It buys you nothing and can actually hide
serious errors if you forget to #include <stdlib.h>. Moreover, the fewer
useless casts in your code the easier it is to read and maintain.
strcpy(*hash_array[n].name, "John Smith");
*hash_array[n].id = 1234;
*hash_array[n].department = "Marketing";
*hash_array[n].salary = 4000;
...

hash_array will contain all the pointers pointing to each object of Employee type.

My questions are:

How to determine MAX_SIZE above?
It depends on your hashing function. How do you calculate the hashing value?
How many possible values are there going to be? That would be your MAX_SIZE.
How to calculate index, n, to locate an appropriate array item? I read
somewhere prime number can be used for such purpose(build a hash function). Could you show me how this work?




这真的不是一个C问题,所以它超出了这个

新闻组的范围。你甚至没有告诉我们你想要以什么为基础进行散列。

它的名字,身份证,甚至工资?不,不打扰回答,把这个问题带到这个问题来编译。编程在哪里(我认为)它更热门。


希望这有帮助,


彼得



This is really not a C question, so it is outside the scope of this
newsgroup. You didn''t even tell us what you want to base the hashing on. Is
it the name, the id, or even the salary? No, don''t bother answering, take
this question to comp.programming where (I think) it is more topical.

Hope this helps,

Peter


al写道:

这就是我的意思试图写一个简单的哈希表:

struct Employee
{char name [30];
int id;
char department [10];
浮动薪水;
};

struct Employee * hash_array [MAX_SIZE];

hash_array [n] =(struct Employee *)malloc(sizeof (员工));
strcpy(* hash_array [n] .name," John Smith");
* hash_array [n] .id = 1234;
* hash_array [n]。 department =" Marketing";
* hash_array [n] .salary = 4000;
...

hash_array将包含指向每个对象的所有指针
员工类型。

我的问题是:

如何确定上面的MAX_SIZE?
如何计算索引n来定位合适的e数组项目?
我读到的某个素数可用于此目的(构建一个哈希函数)。你能告诉我这是怎么回事吗?

Here''s what I am trying to write a simple hash table:

struct Employee
{
char name[30];
int id;
char department[10];
float salary;
};

struct Employee *hash_array[MAX_SIZE];

hash_array[n] = (struct Employee*)malloc(sizeof(Employee));
strcpy(*hash_array[n].name, "John Smith");
*hash_array[n].id = 1234;
*hash_array[n].department = "Marketing";
*hash_array[n].salary = 4000;
...

hash_array will contain all the pointers pointing to each object
of Employee type.

My questions are:

How to determine MAX_SIZE above?
How to calculate index, n, to locate an appropriate array item?
I read somewhere prime number can be used for such purpose(build
a hash function). Could you show me how this work?




这是我写的hashlib的东西。请参阅:


< http://cbfalconer.home.att.net/download/>


这应该可以减少您的问题确定合适的哈希

函数并初始化和销毁​​struct Employee类型。

hashlib将使用极差的哈希函数,尽管

不如具有良好的哈希函数。


-

Chuck F(cb********@yahoo.com)(cb **** ****@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>使用worldnet地址!



This is the sort of thing I wrote hashlib for. See:

<http://cbfalconer.home.att.net/download/>

which should reduce your problems to determining suitable hash
functions and initializing and destroying struct Employee types.
hashlib will function with extremely poor hash functions, albeit
not as well as with good hash functions.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


这篇关于哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆