在C中初始化哈希表

时间:2018-12-14 10:49:20

标签: c hash hashmap hashtable

我正在尝试初始化哈希表以创建字典。该程序会读一本字典和一本书,然后打印出拼写错误的单词。

这是初始化的正确方法吗?

struct dic_elements {
   char *word;
   struct dic_elements *next;
};
typedef struct dic_elements elem;

/*table structure*/
struct dic_table {
   int size;
   elem **h_table[];
};
typedef struct dic_table dic;


/*Allocating memory for hash table*/
dic* dic_init(int size);
{
   dic *d;
   d = (dic*)malloc(sizeof(dic));
   assert(d != NULL);

   d->size = 0;
   d->h_table = elem_init(8);
}

/*create space for array elements and set pointers to null*/
 elem* elem_init(int size)
{
   int i;
   elem *table;

   for (i = 0; i < 8; i++) {
      table = calloc(8, sizeof(elem); 
      assert(e != NULL);
      table[i]->word = NULL;
      table[i]->next = NULL;
   }
}

0 个答案:

没有答案