struct hashLink
{
KeyType key; /*the key is what you use to look up a hashLink*/
ValueType value; /*the value stored with the hashLink, an int in our case*/
struct hashLink *next; /*notice how these are like linked list nodes*/
};
struct hashMap
{
hashLink ** table; /*array of pointers to hashLinks*/
int tableSize; /*number of buckets in the table*/
int count; /*number of hashLinks in the table*/
};
我正在尝试使用 hashLinks 遍历 hashMap。这是正确的方法吗?hashLinks 在一个数组中,并且在一个链表中可能有更多的 hashLinks 链接到它们。我只是不明白如何使用指向指针的指针。tableSize 是数组中元素的数量。在每个数组位置,可能会有更多的 hashLinks 链接到第一个位置。
for(int i = 0; i < ht->tableSize; i ++)
{
hashLink *current;
if (ht->table[i] != 0)
{
current = ht->table[i];
while(current->next !=0)
{
hashLink *next;
next = current->next;
free(current->key);
free(current);
current = next;
}
free(current->key);
free(current);
}
else
{
continue;
}
counter++;
}
}