[英]how to update table size inside hash function
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 我正在学习如何实现哈希表,但在这里我有点困惑,因为在下面的书中可以找到代码,而且我对代码的理解也很充分,但是在书中没有定义
HASH
函数,我知道我们必须通过定义它拥有,但是根据下面被给给予书内码HASH
正在两个参数无论我使用的HASH
像HashInsert
它正在采取两个参数index=HASH(data,t->size)
如果我们假设的那返回类型HASH
现在为int例如,我们可以将HASH
定义为
int HASH(int data,int tsize){
return(data%7);
}
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 但是根据我的程序,我应该如何更新
HASH
函数中的t->size
(表大小)或如何使用它,请帮助我正确实现上述HASH
函数
#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;
}
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
%
. 通常通过对
%
模来完成。
So instead of a fixed magic number 7
you modulo with the size: 因此,您可以将大小取模,而不是固定的幻数
7
:
return(data%tsize);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.