程序填空题:Insert an item into the hash table ht[]
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
```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