将哈希表的大小重新调整为C中的main
我已经成功地创建了一个新的、更大的哈希表,它重新哈希当前表中的数据,并将其插入到一个新表中。然而,事实证明,我想要做的更改是本地反映的,而不是传递回main。在下面代码段的底部,我注释掉了行将哈希表的大小重新调整为C中的main,c,hash,C,Hash,我已经成功地创建了一个新的、更大的哈希表,它重新哈希当前表中的数据,并将其插入到一个新表中。然而,事实证明,我想要做的更改是本地反映的,而不是传递回main。在下面代码段的底部,我注释掉了行**listarray=**listarray2
**listarray=**listarray2因为我不认为那是我想要的。我知道代码的其余部分正常工作,因为在我的insert()函数中,不同的数组位置存储相同的值
我唯一的问题是:如何让listarray2覆盖listarray,以便它作为默认表反映回main中
void resizeTable (node **listarray, int *size)
{
//listarray is an array of node*, declared in main just like listarray2
//size is 8, should reflect the change to 11 back in main
int newSize= 11;
node *temp; //iterating pointer
node **listarray2= malloc (sizeof (node*) * newSize); //create space for a new array of node*'s
for(i=0;i<newSize;i++)
listarray2[i]=NULL; //set everything inside to NULL
//go through original listarray (of size 8)
for(i=0;i<(*size);i++)
{
if (listarray[i]==NULL) //ignore empty array positions
continue;
temp=listarray[i]; //temp now points to a node that is not NULL
while(temp!=NULL) //until the end of the list in that particular array
{
//arg1 is the new list,arg2 is the new Size 11, arg3 is the data from the to-be-old hash table
insert(&*listarray2,newSize,temp->data);
temp=temp->next;
}
}
//free(listarray);
//**listarray=**listarray2;
*size = newSize;
}//end resize()
void可调整大小(节点**listary,int*size)
{
//listarray是一个节点*的数组,与listarray2一样在main中声明
//尺寸为8,应反映出主变回11
int newSize=11;
node*temp;//迭代指针
node**listarray2=malloc(sizeof(node*)*newSize);//为node*的新数组创建空间
对于(i=0;不精确;
}
}
//免费(列表数组);
//**listarray=**listarray2;
*大小=新闻大小;
}//结束调整大小()
为了清晰起见,重命名一些变量时,您可以执行以下操作:
#include "assert.h"
void resizeTable (node ***p_listarray, int *p_size)
{
//listarray is an array of node*, declared in main just like newListarray
//size is 8, should reflect the change to 11 back in main
node **newListarray;
int newSize= 11;
node **oldListarray;
int oldSize;
int i;
assert(p_listarray != NULL); /* From http://en.wikipedia.org/wiki/Assert.h */
assert(p_size != NULL); /* From http://en.wikipedia.org/wiki/Assert.h */
oldListarray = *p_listarray;
oldSize = *p_size;
newListarray = malloc (sizeof (node*) * newSize); //create space for a new array of node*'s
for(i=0;i<newSize;i++)
newListarray[i]=NULL; //set everything inside to NULL
//go through original listarray (of size 8)
for(i=0;i<oldSize;i++)
{
node *temp; //iterating pointer
if (oldListarray[i]==NULL) //ignore empty array positions
continue;
temp=oldListarray[i]; //temp now points to a node that is not NULL
while(temp!=NULL) //until the end of the list in that particular array
{
//arg1 is the new list,arg2 is the new Size 11, arg3 is the data from the to-be-old hash table
insert(&*newListarray,newSize,temp->data);
temp=temp->next;
}
}
*p_listarray = newListarray;
*p_size = newSize;
free(oldListarray);
}//end resize()
通过模拟按引用传递,即通过传递指针。在您的特定示例中,它应该是指向节点
指针的指针,即节点***列表数组
。您还需要另一个间接级别。您当前的实现将新列表的第一个节点复制到旧列表的第一个节点。指针不是魔术!Joachim,你确定你指的是节点***listarray而不是***listarray2吗?我理解你的意思,但实现似乎仍然很棘手。你能告诉我们如何调用resizeTable
?resize(&*listarray,&size);这就是上面的评论所指的另一个间接层次!我将研究它是如何工作的,因为它确实如此!谢谢
node **listarray;
int size;
// Initialize the hash table, and then when it needs resizing...
resizeTable(&listarray, &size);