-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Insert an item into the hash table ht[]

Luz4年前 (2021-05-10)题库536
The function is to insert an item into the hash table ht[] with the hash function hash. Here a list node contains item which is of element type, and a next pointer.

```c++
void insert(element item, list_pointer ht[])
{
int hash_value = hash(item.key);
list_pointer ptr, trail = NULL, lead = ht[hash_value];
for ( ; lead; trail = lead, lead = lead->next) {
if (!strcmp(lead->item.key, item.key)) {
fprintf(stderr, "The key is in the table\n");
exit(1);
}
}
ptr = (list_pointer)malloc(sizeof(list));
@@[ptr->item = item](2);
ptr->next = NULL;
if (trail)
@@[trail->next = ptr](2);
else
@@[ht[hash_value] = ptr](2);
}


```





答案:
第1空:ptr->item = item

第2空:trail->next = ptr

第3空:ht[hash_value] = ptr

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。