使用链表创建字典数据结构。
typedef struct _dictionary_entry_t
{
const char* key;
const char* value;
struct dictionary_entry_t *next;
struct dictionary_entry_t *prev;
} dictionary_entry_t;
typedef struct _dictionary_t
{
dictionary_entry_t *head;
dictionary_entry_t *curr;
int size;
} dictionary_t;
处理将字典条目添加到链表的功能。
int dictionary_add(dictionary_t *d, const char *key, const char *value)
{
if (d->curr == NULL) //then list is empty
{
d->head = malloc(sizeof(dictionary_entry_t));
d->head->key = key; //set first dictionary entry key
d->head->value = value; //set first dictionary entry value
d->head->next = NULL;
//d->curr = d->head;
}
else
{
d->curr = d->head;
while (strcmp((d->curr->key), key) != 0 && d->curr != NULL) //while keys don't match and haven't reached end of list...
{
d->curr = d->curr->next;
}
}
return -1;
}
将 d->curr 分配给 d->curr->next 会给我警告“来自不兼容的指针类型的分配”。
我的错误是什么?curr 和 next 都是 *dictionary_entry_t 类型