我目前正试图建立一个基于哈希表的字典。
其逻辑是:
有一个名为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;
typedef struct
{
pList* listFirst;
} _HashArray;
typedef struct
{
_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;
}
所有可更新->显示错误。
最佳答案
你的声明pHash
引用了一个不存在的类型struct _Hash
。
更改此:
typedef struct
{
_HashArray* HashArray;
HashFunc HashFunc;
PrintFunc PrintEntry;
CompareFunc CompareWords;
GetKeyFunc GetEntryKey;
DestroyFunc DestoryEntry;
int TableSize;
} _Hash;
对此:
typedef struct _Hash
{
_HashArray* HashArray;
HashFunc HashFunc;
PrintFunc PrintEntry;
CompareFunc CompareWords;
GetKeyFunc GetEntryKey;
DestroyFunc DestoryEntry;
int TableSize;
} _Hash;
原始声明创建了一个匿名结构,typedef将其定义为
_Hash
,但是没有任何东西创建struct _Hash
,因此pHash
的原始typedef仍然不完整。然后您就遇到了
_Hash
没有DestroyEntry
成员的问题(您在struct
中有一个输入错误)。要使问题更清楚,请阅读以下内容:
typedef struct _Hash *pHash;
因此:
#define pHash struct _Hash *
您将看到您的结构定义类似于
#define _Hash struct { ... }
它没有命名结构。所以在你的程序中没有
struct _Hash
这样的东西。随着我的变化
#define _Hash struct _Hash { ... }