【发布时间】:2015-08-12 01:00:40
【问题描述】:
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++;
}
}
【问题讨论】:
-
我不会完全这样做,但类似。也就是说,我一看就觉得没问题。哦,在 C 中你真的应该使用
NULL作为空指针。 -
你是认真的吗?因为我不知道自己在做什么......
-
添加到我之前的评论中,一目了然如果所有指针都正确分配或以其他方式初始化。 :) 至于指针的问题,看成指针数组就好了。
-
挑剔:
array of pointers to hashLinks-->pointer to pointer to hashLinks -
while(current->next !=0)-->>while(current != NULL)
标签: c pointers struct pointer-to-pointer