【问题标题】:Defining list and inner dictionary as the value of a dictionary in c将列表和内部字典定义为c中字典的值
【发布时间】:2018-12-27 00:26:39
【问题描述】:

我是 c 语言的新手,如果我的问题太基本,我很抱歉。

我想在c 中定义一个字典,其中有一个列表作为键的值。换句话说,我喜欢在c中拥有类似python的东西:

my_dictionary = {1:{'name':'john','items':['item1','item2']},2:{'name':'bob','items':['item3','item4']}}

然后我喜欢访问我定义的字典,如下所示: my_item = my_dictionary[1]['items'].

我知道这在 python 中很容易,但是对于c,我找不到一个很好的例子。我能够定义简单的字典,例如:

typedef struct dict_t_struct {
    char *key;
    void *value;
    struct dict_t_struct *next;
} dict_t;

我可以使用以下函数轻松地添加、删除或打印字典中的项目:

dict_t **dictAlloc(void) {
    return malloc(sizeof(dict_t));
}

void dictDealloc(dict_t **dict) {
    free(dict);
}

void *getItem(dict_t *dict, char *key) {
    dict_t *ptr;
    for (ptr = dict; ptr != NULL; ptr = ptr->next) {
        if (strcmp(ptr->key, key) == 0) {
            return ptr->value;
        }
    }

    return NULL;
}

void delItem(dict_t **dict, char *key) {
    dict_t *ptr, *prev;
    for (ptr = *dict, prev = NULL; ptr != NULL; prev = ptr, ptr = ptr->next) {
        if (strcmp(ptr->key, key) == 0) {
            if (ptr->next != NULL) {
                if (prev == NULL) {
                    *dict = ptr->next;
                } else {
                    prev->next = ptr->next;
                }
            } else if (prev != NULL) {
                prev->next = NULL;
            } else {
                *dict = NULL;
            }

            free(ptr->key);
            free(ptr);

            return;
        }
    }

但问题是我需要将链表作为字典的值和字典中的内部字典。

【问题讨论】:

  • 这只是你在值中存储的一个例子,不是吗?从外观上看(尽管您没有向我们展示您的 put 函数),这应该已经可以正常工作了,放置链接列表,然后在合适的情况下将字典存储在列表中。您需要一种比 python 稍微复杂的读取值的方法,但假设您确定要查找的索引是好的,它仍然可能是一行。
  • 您在结构中缺少type 成员。 type 成员的目的是指定 value 成员的类型,例如,可以是 int *char * 或指向字典的指针。
  • 但这在 C++ 中会容易得多,几乎和 Python 一样简单,如果你可以使用 C++ 代替?
  • 旁注:您找到(或编写?)的代码在大多数语言/框架中没有实现所谓的“字典” - 通常“字典”是指通过键查找 O(1) 的哈希表 -帖子中显示的代码只是链表......
  • 我不能使用 c++。你能帮我如何在我定义的字典中定义一个内部字典,然后我怎样才能访问我的项目?

标签: c dictionary struct linked-list


【解决方案1】:

1 你的结构是json,不能完全使用普通字典

2 如果需要存储列表,需要定义列表的数据结构。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DICT_STRING 1
#define DICT_LIST 2

typedef struct list_node {
    void* value;
    struct list_node* next;
} list_node;

typedef struct list {
    list_node* head;
} list;

typedef struct dict_entry {
    int type;
    char* key;
    void* value;
    struct dict_entry* next;
} dict_entry;

typedef struct dict_t {
    dict_entry* head;
} dict_t;

dict_t* dictAlloc(void) {
    dict_t* d = malloc(sizeof(dict_t));
    d->head = NULL;
    return d;
}

void dictDealloc(dict_t* dict) {
    free(dict);
}

dict_entry* addItem(dict_t* dict, int type, char* key, void* value) {
    dict_entry* de = malloc(sizeof(*de));
    de->type = type;
    de->key = key;
    de->value = value;
    de->next = dict->head;
    dict->head = de;
    return de;
}

dict_entry* getItem(dict_t* dict, char* key) {
    dict_entry* de = dict->head;
    while (de) {
        if (strcmp(de->key, key) == 0) {
            return de;
        }

        de = de->next;
    }

    return NULL;
}

int main(int argc, char** argv) {
    dict_t* d = dictAlloc();
    dict_entry* de;

    list* l = malloc(sizeof(*l));
    list_node* n = malloc(sizeof(*n));
    n->value = "value";
    l->head = n;

    addItem(d, DICT_LIST, "key", l);
    de = getItem(d, "key");
    switch (de->type) {
        case DICT_LIST: {
            list* l = de->value;
            printf("%s", l->head->value);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多