如何在哈希函数中更新表大小 [英] how to update table size inside hash function
问题描述
我正在学习如何实现哈希表,但在这里我有点困惑,因为在下面的书中可以找到代码,而且我对代码的理解也很充分,但是书中没有HASH
函数的定义,我知道我们必须自己定义它,但是根据下面给出的代码,在书HASH
中给与的任何参数都接受两个参数,就像我在HashInsert
中那样使用HASH
时,如果我们假设return HASH
的类型为int
现在,例如,我们可以将HASH
定义为
I was learning how to implement Hash table but i am bit confused here in because in the book below code was available and i understood well enough the code, but inside book there was no definition of HASH
function ,i know we have to define it by own ,but according the below code that was given give inside book HASH
is taking two arguments wherever i used the HASH
like in HashInsert
it is taking two arguments index=HASH(data,t->size)
if we assume that return type of HASH
as int
now for eg we can define HASH
as
int HASH(int data,int tsize){
return(data%7);
}
但是根据我的程序,我应该如何更新HASH
函数中的t->size
(表大小),或者应该如何使用
请帮助我正确实现上述HASH
函数
but according to my program how should i update t->size
(table size) inside HASH
function or how should i use that
Please help me in proper implementation of the above HASH
function
#define Load_factor 20
#include<stdio.h>
#include<stdlib.h>
struct Listnode{
int key;
int data;
struct Listnode* next;
};
struct HashTableNode{
int bcount; /// Number of elements in block
struct Listnode* next;
};
struct HashTable{
int tsize; /// Table size
int count;
struct HashTableNode** Table;
};
struct HashTable* createHashTable(int size){
struct HashTable* h;
h=(struct HashTable*)malloc(sizeof(struct HashTable));
h->tsize=size/Load_factor;
h->count=0;
h->Table=(struct HashTableNode**)malloc(sizeof(struct HashTableNode*)*h->tsize);
if(!h->Table){
printf("Memory Error");
return NULL;
}
for(int i=0;i<h->tsize;i++){
h->Table[i]->bcount=0;
h->Table[i]->next=NULL;
}
return h;
}
/// Hashsearch
int HashSearch(struct HashTable* h,int data){
struct Listnode* temp;
temp=h->Table[HASH(data,h->tsize)]->next;
while(temp) ///same as temp!=NULL
{
if(temp->data==data)
return 1;
temp=temp->next;
}
return 0;
}
int HashDelete(struct HashTable* h,int data)
{
int index;
struct Listnode *temp,*prev;
index=HASH(data,h->tsize);
for(temp=h->Table[index]->next,prev=NULL;temp;prev=temp,temp=temp->next)
{
if(temp->data==data)
{
if(prev!=NULL)
prev->next=temp->next;
free(temp);
h->Table[index]->bcount--;
h->count--;
return 1;
}
}
return 0;
}
int HashInsert(struct HashTable *h ,int data){
int index;
struct Listnode* temp,*newnode;
if(HashSearch(h,data))
return 0;
index = HASH(data,h->tsize);
temp=h->Table[index]->next;
newnode=(struct Listnode*)malloc(sizeof(struct Listnode));
if(!newnode)
return -1;
newnode->key=index;
newnode->data;
newnode->next=h->Table[index]->next;
h->Table[index]->next=newnode;
h->Table[index]->bcount++;
h->count++;
return 1;
}
我只是在学习哈希的实现,所以main看起来很奇怪
i am just learning implementation of hashing so main is looking quiet weird
int main(){
return 0;
}
推荐答案
您不应该!我的意思是您不应该修改它.
You shouldn't! I mean you shouldn't modify it.
该函数获取哈希表的大小(存储桶"的数量),因此可以使用它从哈希值创建存储桶索引.通常通过对%
取模来完成.
Instead the function gets the size of the hash-table (the number of "buckets") so it can use it to create a bucket index from the hash value. This is typically done through modulo %
.
因此,您可以用大小:
return(data%tsize);
这篇关于如何在哈希函数中更新表大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!