c/c++语言开发共享如何更新散列函数中的表大小

我正在学习如何实现哈希表但我在这里有点困惑因为在下面的书中代码是可用的并且我对代码的理解得很好,但是在书中没有HASH函数的定义,我知道我们必须通过拥有,但根据下面给出的代码给出内部书HASH是两个参数,无论我在HashInsert使用HASH index=HASH(data,t->size)如果我们假设返回类型的HASHHashInsert它采用两个参数index=HASH(data,t->size)现在为int,例如我们可以将HASH定义为

 int HASH(int data,int tsize){ return(data%7); } 

但是根据我的程序,如何在HASH函数内更新t->size (表大小)或者我应该如何使用它请帮助我正确实现上面的HASH函数

 #define Load_factor 20 #include #include 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;itsize;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; } 

我正在学习哈希的实现,因此主要看起来很安静

 int main(){ return 0; } 

    不应该! 我的意思是你不应该修改它。

    相反,该函数获取散列表的大小(“桶”的数量),因此它可以使用它从散列值创建桶索引。 这通常通过模数%来完成。

    因此,不是固定的幻数 7而是以大小为模:

     return(data%tsize); 

      以上就是c/c++开发分享如何更新散列函数中的表大小相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519045.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐