【发布时间】:2021-11-16 17:02:02
【问题描述】:
这是哈希表结构:
struct hash_table
{
entry_t buckets[No_Buckets];
};
这是入口结构:
struct entry
{
int key; // holds the key
char *value; // holds the value
entry_t *next; // points to the next entry (possibly NULL)
};
该函数的目的是返回哈希表的大小(它包含多少个 !NULL 条目),但我不确定我应该如何递归地编写它。
int hash_table_size(hash_table_t *ht)
{
int counter = 0;
for (int i = 0; i < No_Buckets; i++)
{
entry_t *current_entry = (ht->buckets[i]).next;
if (current_entry != NULL)
{
counter++ ;
}
}
return counter;
}
【问题讨论】:
-
为什么你认为这个任务需要递归?我认为循环可以很好地完成工作,只要进行一些调整。
-
您可以通过循环轻松替换
if(..) ...,而无需递归。 -
@RefugnicEternium 仅用于学习目的,我认为此功能将是最简单的尝试:)。
-
如何确定存储桶是否正在使用? (目前您只计算携带一连串后代的桶)
-
@davdavdav2 数组是固定大小的。条目永远不会为空。你如何检测到一个条目 in use ?
标签: c recursion struct hashtable