我目前正在尝试基于哈希表构建字典。逻辑是:有一个名为 HashTable 的结构,其中包含以下内容:
HashFunc HashFunc;
PrintFunc PrintEntry;
CompareFunc CompareWords;
GetKeyFunc GetEntryKey;
DestroyFunc DestoryEntry;
这些是指向函数的指针(用户创建以制作模块化字典)。
_HashArray* HashArray;
int TableSize;
HashArray 是 _HashArray 对象的数组 -> 每个都是链表的第一个元素。TableSize 是 HashArray 的大小(我们能够创建的哈希值的数量)。
哈希.h:
typedef enum {FAIL = 0, SUCCESS} Result;
typedef enum {SAME = 0, DIFFERENT} CompResult;
typedef struct _Hash *pHash;
typedef void* pElement;
typedef void* pKey;
typedef int (*HashFunc) (pKey key, int size);
typedef Result (*PrintFunc) (pElement element);
typedef CompResult (*CompareFunc) (pKey key1, pKey key2);
typedef pKey (*GetKeyFunc) (pElement element);
typedef void (*DestroyFunc)(pElement element);
哈希.c
typedef struct _List
{
pElement _Element;
struct _List* listNext;
} pList;
cấu trúc typedef
{
pList* listFirst;
} _HashArray;
cấu trúc typedef
{
_HashArray* HashArray;
HashFunc HashFunc;
PrintFunc PrintEntry;
CompareFunc CompareWords;
GetKeyFunc GetEntryKey;
DestroyFunc DestoryEntry;
int TableSize;
} _Hash;
我正在尝试减速:
pHash HashCreate(int ArraySize, void* HashWord, void* PrintEntry, void* CompareWords, void* GetEntryKey, void* DestroyEntry)
{
// First set all function pointers
// Create the hashtable
pHash newTable = (pHash)malloc(sizeof(_Hash));
newTable->HashArray = (_HashArray*)malloc(sizeof(_HashArray)*ArraySize);
newTable->TableSize = ArraySize;
newTable->HashFunc = HashWord;
newTable->PrintEntry = PrintEntry;
newTable->CompareWords = CompareWords;
newTable->GetEntryKey = GetEntryKey;
newTable->DestroyEntry = DestroyEntry;
}
所有newTable->显示错误。
每个结构定义都需要一个名称。查看您对 _List 的定义:
typedef struct _List {
pElement _Element;
struct _List* listNext;
} pList;
以上等同于:
struct _List {
pElement _Element;
struct _List* listNext;
}
typedef struct _List pList;
对于所有的 struct/typedef 定义,您应该始终遵循上述格式之一。 _Hash 和 _HashArray 的 typdef 指的是未命名的结构。
typedef struct HashArray_struct_name_goes_here
{
pList* listFirst;
} _HashArray;
typedef struct Hash_struct_name_goes_here
{
_HashArray* HashArray;
HashFunc HashFunc;
PrintFunc PrintEntry;
CompareFunc CompareWords;
GetKeyFunc GetEntryKey;
DestroyFunc DestoryEntry;
int TableSize;
} _Hash;
Tôi là một lập trình viên xuất sắc, rất giỏi!