如何在c中创建哈希表的动态数组
我设置了以下bucket条目结构和哈希表如何在c中创建哈希表的动态数组,c,arrays,hashtable,dynamic-arrays,C,Arrays,Hashtable,Dynamic Arrays,我设置了以下bucket条目结构和哈希表 typedef struct Hash_Entry { struct Hash_Entry *next; void *key_Data; unsigned key_hash; char key[5]; } Hash_Entry; typedef struct Hash_Table { struct Hash_Entry **bucketPtr; /* Bu
typedef struct Hash_Entry
{
struct Hash_Entry *next;
void *key_Data;
unsigned key_hash;
char key[5];
} Hash_Entry;
typedef struct Hash_Table
{
struct Hash_Entry **bucketPtr; /* Buckets in the table */
int size; /* Actual size of array. */
int numEntries; /* Number of entries in the table. */
int mask; /* Used to select bits for hashing. */
} Hash_Table;
我想为该哈希表创建一个数组(或动态数组),这样当我觉得该表已满时,我可以创建另一个表,而不是重新调整其大小您可以使用stdlib中的malloc创建一个数组
Hash_Table* array = (Hash_Table*)malloc(sizeof(Hash_Table) * 100);
当数组已满时,您可以执行realloc
你可以看看:
您可以使用stdlib中的malloc创建一个数组
Hash_Table* array = (Hash_Table*)malloc(sizeof(Hash_Table) * 100);
当数组已满时,您可以执行realloc
你可以看看:
类似于:
void hash_table_init(Hash_Table *table, size_t entries)
{
size_t i;
table->size = 0;
table->numEntries = entries;
table->bucketPtr = malloc(table->numEntries * sizeof *table->bucketPtr);
for(i = 0; i < table->numEntries; i++)
table->bucketPtr[i] = NULL;
table->mask = 0; /* Not sure how to initialize this. */
}
假设实际将使用大多数桶,那么为什么不使用它们呢 类似于:
void hash_table_init(Hash_Table *table, size_t entries)
{
size_t i;
table->size = 0;
table->numEntries = entries;
table->bucketPtr = malloc(table->numEntries * sizeof *table->bucketPtr);
for(i = 0; i < table->numEntries; i++)
table->bucketPtr[i] = NULL;
table->mask = 0; /* Not sure how to initialize this. */
}
假设实际将使用大多数桶,那么为什么不使用它们呢 他说他不想调整尺寸,他说他不想调整尺寸。