I'm trying to implement a binary search tree that holds an inventory of ordered stock. The stocked item attributes are stored in nodes as such:
我正在尝试实现一个二叉搜索树,其中包含有序库存的库存。库存商品属性存储在节点中:
typedef struct item item_t;
struct item{
char name;
int price;
int quantity;
item_t *left;
item_t *right;
};
The idea is to prompt a user to enter the above attributes, and then add the entered item to a node. This is what I've written so far:
想法是提示用户输入上述属性,然后将输入的项添加到节点。这是我到目前为止所写的:
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;
}
I want root
to always be pointing to the first item ever entered, and current_leaf
to be pointing to the last item processed. compare
returns 1 if the item being processed (input
) is less than the last processed item (current_leaf
). update_item
is what sets the data for the new nodes (leaves).
我希望root始终指向已输入的第一个项目,并且current_leaf指向最后处理的项目。如果正在处理的项目(输入)小于最后处理的项目(current_leaf),则compare返回1。 update_item用于设置新节点(叶子)的数据。
The above isn't fully complete, but it's what I'm up to at the moment. I'm struggling to work out how to write add_node
and how to keep current_leaf
updated correctly.
以上并不完全,但这是我现在所要做的。我正在努力研究如何编写add_node以及如何正确更新current_leaf。
When I try to compile I get the following errors:
当我尝试编译时,我收到以下错误:
$ 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;
^
1 个解决方案
#1
item_t create_node(char *input)
should be
item_t *create_node(char *input)
What you return is a structure but you should be returning pointer of type struct item
.
你返回的是一个结构,但你应该返回struct item类型的指针。
#1
item_t create_node(char *input)
should be
item_t *create_node(char *input)
What you return is a structure but you should be returning pointer of type struct item
.
你返回的是一个结构,但你应该返回struct item类型的指针。