c - 实现二叉搜索树 - "incompatible types when returning type ' struct item_t *'..."

标签 c data-structures struct binary-search

我正在尝试实现一个二叉搜索树来保存订购库存。库存商品属性存储在这样的节点中:

typedef struct item item_t;
struct item{
    char name;
    int price;
    int quantity;
    item_t *left;
    item_t *right;
};

想法是提示用户输入上述属性,然后将输入的项目添加到节点。这是我到目前为止写的:

item_t *root = NULL;
item_t *current_leaf = NULL;

void prompt_user(){
    /*
    In here contains the code that prompts the user for the item attributes
    and stores it in a variable called input
    */
    insert_node(input);
}

void insert_node(char *input){
    /*If tree doesnt have a root...*/
    if (root == NULL){

        /*Create one...*/
        root = create_node(input);
    }

    else{
        item_t *cursor = root;
        item_t *prev = NULL;
        int is_left = 0;
        int comparison;

        while(cursor != NULL){

            /*comparison will be 1 is the key of input is less than the key   
            of the cursor, and 2 otherwise...*/
            comparison = compare(input, cursor);
            prev = cursor;

            if(comparison == 1){
                is_left = 1;
                cursor = cursor->left;
            }
            else if (comparison == 2){
                is_left = 0;
                cursor = cursor->right;
            }
        }

        if(is_left){
            *prev->left = create_node(input);
            current_leaf = prev->left;
        }
        else{
            *prev->right = create_node(input);
            current_leaf = prev->right;
        }
    }
}

item_t create_node(char *input){

    item_t *new_node = (item_t*)malloc(sizeof(item_t));

    if (new_node == NULL){
        printf("Out of memory. Shutting down.\n");
        exit(EXIT_FAILURE);
    }

    /*Add data to the node...*/
    update_item(input, new_node);

    new_node->left = NULL;
    new_node->right = NULL;

    current_leaf = new_node;

    return new_node;
}

我希望 root 始终指向输入的第一个项目,而 current_leaf 指向最后处理的项目。如果正在处理的项 (input) 小于最后处理的项 (current_leaf),compare 返回 1。 update_item 是为新节点(叶子)设置数据的内容。

以上内容并不完整,但这是我目前正在做的事情。我正在努力研究如何编写 add_node 以及如何保持 current_leaf 正确更新。

当我尝试编译时出现以下错误:

$ gcc -ansi -pedantic -Wall -o proj2.exe proj2.c
 proj2.c: In function 'insert_node':
 proj2.c:416:14: error: incompatible types when assigning to type'structitem_t *' from type 'item_t'
         root = create_node(input);
               ^
 proj2.c: In function 'create_node':
 proj2.c:470:5: error: incompatible types when returning type 'struct item_t *' but 'item_t' was expected
      return new_node;
      ^

最佳答案

item_t create_node(char *input)

应该是

item_t *create_node(char *input)

您返回的是一个结构,但您应该返回类型为 struct item 的指针。

关于c - 实现二叉搜索树 - "incompatible types when returning type ' struct item_t *'...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30189037/

相关文章:

c - 如何在 C 中使用 switch 语句保留被操作位的值

c - 我如何判断我机器上的每个核心是否使用相同的计时器?

C++ 自定义列表迭代器错误

c - 使用结构实例指针取消引用结构实例元素

c++ - 另一个 : Passing Vector of Structs to Function - C++, MinGW

c - Z3:提取数组解释

c - 如何减少下面代码执行的时间?

javascript - 如何通过索引数组重新排列数组?

java - Java中如何存储和重用一组相关的常量值?

c++ - C/C++ : Pointers within Const Struct