简体   繁体   English

如何在没有双指针的情况下更改 function 内的结构?

[英]How do I change my struct within function without double pointers?

I'm trying to implement a hash table that contains contacts, so every line of the hash table is a chained list.我正在尝试实现一个包含联系人的 hash 表,因此 hash 表的每一行都是一个链表。 When the hash table has too many contacts, I have to resize it.当 hash 表的联系人太多时,我不得不调整它的大小。 However, I don't have a say in the functions' prototypes, meaning the function resize always takes a single pointer to my hash table as an argument.但是,我对函数的原型没有发言权,这意味着 function 调整大小始终采用指向我的 hash 表的单个指针作为参数。

These are my structures:这些是我的结构:

struct dir {
uint32_t len;
uint32_t contactsNumber;
struct contact* contactList;
};

struct contact {
char *name;
char *num;
struct contact *next;
};

This is how I initialize my directory:这是我初始化目录的方式:

struct dir *dir_create(uint32_t len)
{
struct dir * directory = malloc(sizeof(struct dir)*len);
directory->contactsNumber = 0;

for (uint32_t i = 0; i<len;i++) {
  directory[i].contactList = contact_sentinel();
}

directory->len = len;
return(directory);
}

contact_sentinel() returns a chained list with an empty dummy node. contact_sentinel() 返回一个带有空虚拟节点的链表。

This is my resize prototype (imposed):这是我的调整大小原型(强加):

extern void dir_resize(struct dir *dir,uint32_t new_size);

Obviously, when I try to manipulate my struct within the resize function, everything works well and the table is resized, however as soon as I leave the function, my directory goes back to normal.显然,当我尝试在调整大小 function 内操作我的结构时,一切正常并且表大小调整,但是一旦我离开 function,我的目录就会恢复正常。 The reason is that I'm not using a double pointer, meaning my directory still points to the same old directory and nothing was changed.原因是我没有使用双指针,这意味着我的目录仍然指向同一个旧目录并且没有任何改变。

So my question is: How can I resize effectively without using a double pointer?所以我的问题是:如何在不使用双指针的情况下有效地调整大小? Should I change something in my structure?我应该改变我的结构吗? Are there ways to do it?有办法吗?

This is my resize function:这是我的调整大小 function:

void dir_resize(struct dir *dir,uint32_t new_size)
{
uint32_t old_size = dir->len;
struct dir *new_dir = dir_create(new_size);

for (uint32_t i = 0; i<old_size;i++) {
      struct contact *current_contact = dir[i].contactList;
      while (current_contact->next != NULL) {
          dir_insert(new_dir, current_contact->next->name,current_contact->next->num);
          // contact_removeTop(current_contact->next);
          current_contact->next = current_contact->next->next;
      }
    }
  }

I don't know what to do after that, now that I've created the new hash table (by calculating the new hash and inserting it into the right spot), how do I transfer the new_dir to the old dir with this kind of prototype?我不知道在那之后要做什么,现在我已经创建了新的 hash 表(通过计算新的 hash 并将其插入正确的位置),我如何将 new_dir 转移到旧目录原型?

Thanks for your answer Reinstate Monica!感谢您的回答恢复莫妮卡!

Unfortunately doing dir->contactList = new_dir didn't change the table as it reverted back to what it was outside of the function.不幸的是,执行 dir->contactList = new_dir 并没有更改表,因为它恢复到 function 之外的状态。

However, this is my updated resize function:但是,这是我更新后的调整大小 function:

void dir_resize(struct dir *dir,uint32_t new_size)
{
uint32_t old_size = dir->len;
struct dir *new_dir = dir_create(new_size);

for (uint32_t i = 0; i<old_size;i++) {
      struct contact *current_contact = dir[i].contactList;
      while (current_contact->next != NULL) {
          dir_insert(new_dir, current_contact->next->name, current_contact->next->num);
          // contact_removeTop(current_contact->next);
          current_contact->next = current_contact->next->next;
      }
    }

  
dir_free(dir);
dir->contactsNumber = new_dir->contactsNumber;
dir->len = new_size;
for (uint32_t i = 0; i<new_size;i++) {
    dir[i].contactList = new_dir[i].contactList;
}
}

The lines I added at the end seem to work.我在最后添加的行似乎有效。 And my table is changed outside of the function. Now I just gotta figure out how to free the memory space previously used by dir.我的表在 function 之外发生了变化。现在我只需要弄清楚如何释放 dir 之前使用的 memory 空间。 (idk if free(dir) is enough since it has *contacts within it). (idk if free(dir) 就足够了,因为它里面有 *contacts)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM