C 如何在哈希表中保存内存?
在我的结构C 如何在哈希表中保存内存?,c,data-structures,hash,C,Data Structures,Hash,在我的结构struct ListNode中,我正在生成一个int类型的变量键,但是有必要在struct ListNode中生成它,我们可以在HashTableNode中生成它吗?因为当HashTableNode中有两个或多个项目时(也就是说,在单个表节点中冲突会更多)然后,我们必须创建更多的链表节点,如果我们可以在HashTableNode中定义该节点,则每次在该节点中,键变量都会消耗一些内存,而不是节省内存 提到每个列表节点中的键是否正确,以便我们可以在需要时随时访问,因为下面的哈希表实现来自
struct ListNode
中,我正在生成一个int类型的变量键,但是有必要在struct ListNode
中生成它,我们可以在HashTableNode
中生成它吗?因为当HashTableNode
中有两个或多个项目时(也就是说,在单个表节点中冲突会更多)然后,我们必须创建更多的链表节点,如果我们可以在HashTableNode中定义该节点,则每次在该节点中,键变量都会消耗一些内存,而不是节省内存
提到每个列表节点中的键是否正确,以便我们可以在需要时随时访问,因为下面的哈希表实现来自非常著名的数据结构书籍
请告诉我我上面提到的是正确的因为我是初学者,如果不是,请纠正我
#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;
}
int HASH(int data,int tsize){
return(data%tsize);
}
/// 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;
}
#定义荷载系数20
#包括
#包括
结构列表节点{
int键;
int数据;
结构Listnode*next;
};
结构哈希表节点{
int bcount;///块中的元素数
结构Listnode*next;
};
结构哈希表{
int tsize;///表大小
整数计数;
结构HashTableNode**表;
};
结构哈希表*createHashTable(整数大小){
结构哈希表*h;
h=(结构哈希表*)malloc(sizeof(结构哈希表));
h->T尺寸=尺寸/荷载系数;
h->count=0;
h->Table=(struct HashTableNode**)malloc(sizeof(struct HashTableNode*)*h->tsize);
如果(!h->Table){
printf(“内存错误”);
返回NULL;
}
for(int i=0;itsize;i++){
h->Table[i]->bcount=0;
h->Table[i]->next=NULL;
}
返回h;
}
int散列(int数据,int tsize){
返回(数据%t大小);
}
///哈希搜索
int HashSearch(结构哈希表*h,int数据){
结构列表节点*temp;
temp=h->Table[散列(数据,h->tsize)]->下一步;
while(temp)///与temp!相同=NULL
{
如果(临时->数据==数据)
返回1;
温度=温度->下一步;
}
返回0;
}
int HashDelete(结构哈希表*h,int数据)
{
整数指数;
结构列表节点*temp,*prev;
索引=散列(数据,h->tsize);
对于(temp=h->Table[index]->next,prev=NULL;temp;prev=temp,temp=temp->next)
{
如果(临时->数据==数据)
{
如果(上一个!=NULL)
上一个->下一个=临时->下一个;
免费(临时);
h->Table[index]->bcount--;
h->计数--;
返回1;
}
}
返回0;
}
int HashInsert(结构哈希表*h,int数据){
整数指数;
结构Listnode*temp,*newnode;
if(哈希搜索(h,数据))
返回0;
索引=散列(数据,h->tsize);
temp=h->表格[索引]->下一步;
newnode=(struct Listnode*)malloc(sizeof(struct Listnode));
如果(!newnode)
返回-1;
newnode->key=索引;
新建节点->数据;
newnode->next=h->Table[index]->next;
h->Table[index]->next=newnode;
h->Table[index]->bcount++;
h->count++;
返回1;
}
必须为每个节点存储密钥,因为它用于解决冲突。请注意,冲突发生在散列值和非键上,这意味着同一个bucket(HashTableNode
)中的每个元素(Listnode
)仍将具有不同的键,因此无法对其进行优化
但是,在您的示例中,数据是键(通常称为HashSet,而不是HashMap),因此在
列表节点
中确实不需要键
字段,我对键
和索引
感到困惑,因为在我的代码中,键被分配给了索引,您能解释一下吗it@david12345,请查看更新的答案。此外,索引存储到键
字段这一事实对我来说也没有什么意义。