【问题标题】:how to update table size inside hash function如何更新哈希函数内的表大小
【发布时间】:2019-02-11 17:25:55
【问题描述】:

我正在学习如何实现哈希表,但我在这里有点困惑,因为在下面的书中代码可用并且我对代码理解得足够好,但在书中没有HASH函数的定义,我知道我们必须自己定义它,但根据下面给出的代码给出的书内HASH在我使用HASH的任何地方都采用两个参数,就像在HashInsert中一样,它采用两个参数index=HASH(data,t->size)如果我们假设返回HASH 的类型为 int 现在例如,我们可以将HASH 定义为

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

但是根据我的程序,我应该如何在 HASH 函数中更新 t->size(table 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;
}

我只是在学习散列的实现,所以 main 看起来很奇怪

int main(){
  return 0;
}

【问题讨论】:

    标签: c data-structures hash


    【解决方案1】:

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

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

    所以不是固定的magic number 7 你用大小取模:

    return(data%tsize);
    

    【讨论】:

    • 在我的结构 struct ListNode 中,我正在创建一个 int 类型变量 key,但有必要在 struct Listnode 中创建它,我们可以在 HashTableNode 中创建它,因为当两个或多个项目将存在于 HashTableNode 中(即单个表节点中的冲突将更多)而不是我们必须创建更多的链表节点,并且每次在该节点内 key 变量将消耗一些内存,如果我们可以在 HashTableNode 内定义它比我们可以节省内存请告诉我我上面提到的是正确的因为我是初学者如果不是那么请纠正我谢谢你
    • @david12345 对于您当前显示的代码,ListNode-&gt;key 是存储桶索引,从未使用过。相反,哈希表的键是数据,这使得哈希表成为 set。此外,每个桶应该是一个列表(用于冲突),但HashListNode 应该包含键和数据本身,而不是指向其他列表的指针。除非我遗漏了什么,我可能会这样做,因为我们没有您的要求或设计。
    • 因为 keyindex 是不同的,那么为什么在我的代码中它们的索引被分配给键,该代码是在一本著名的 DS 书中给出的
    • 我对@9​​87654335@ 和index 感到困惑,因为在我的代码中,键被分配给索引,你能解释一下吗
    • 如果需要更正我的代码(如果你认为)请更正没有人帮助我我正在与哈希作斗争 3 天请帮助我谢谢先生
    猜你喜欢
    • 2020-09-17
    • 2015-07-13
    • 1970-01-01
    • 2013-12-21
    • 2011-12-10
    • 2014-05-09
    • 2011-04-03
    • 2010-09-21
    相关资源
    最近更新 更多