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

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

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));
      (3分);
      ptr->next = NULL;
      if (trail)
         (3分);
      else
         (3分);
   }
   return ret;
}

猜你喜欢

转载自www.cnblogs.com/srs7665/p/12897922.html