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

程序填空题:散列插入

Luz4年前 (2021-05-10)题库3636
下列代码的功能是利用散列函数`hash`将一个元素插入到散列表`ht[]`中。其中`list`类型的结点包含`element`类型的项`item`、以及一个`next`指针。如果插入成功,则函数返回1,否则返回0。

```c++
int insert( struct element item, list_pointer ht[] )
{
int ret, hash_value;
list_pointer ptr, trail, lead;

ret = 1;
hash_value = hash(item.key);
trail = NULL; lead = ht[hash_value];
for ( ; lead; trail = lead, lead = lead->next) {
if (!strcmp(lead->item.key, item.key)) {
printf("The key is in the table\n");
ret = 0;
}
}
if (ret) {
ptr = (list_pointer)malloc(sizeof(struct list));
@@[ptr->item = item](3);
ptr->next = NULL;
if (trail)
@@[trail->next = ptr](3);
else
@@[ht[hash_value] = ptr](3);
}
return ret;
}
```





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

第2空:trail->next = ptr

第3空:ht[hash_value] = ptr

发表评论

访客

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