c - implementing a binary search tree - "incompatible types when returning type 'struct item_t *'..." -
i'm trying implement binary search tree holds inventory of ordered stock. stocked item attributes stored in nodes such:
typedef struct item item_t; struct item{ char name; int price; int quantity; item_t *left; item_t *right; };
the idea prompt user enter above attributes, , add entered item node. i've written far:
item_t *root = null; item_t *current_leaf = null; void prompt_user(){ /* in here contains code prompts user item attributes , stores in variable called input */ insert_node(input); } void insert_node(char *input){ /*if tree doesnt have 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 1 key of input less key of cursor, , 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 node...*/ update_item(input, new_node); new_node->left = null; new_node->right = null; current_leaf = new_node; return new_node; }
i want root
pointing first item ever entered, , current_leaf
pointing last item processed. compare
returns 1 if item being processed (input
) less last processed item (current_leaf
). update_item
sets data new nodes (leaves).
the above isn't complete, it's i'm @ moment. i'm struggling work out how write add_node
, how keep current_leaf
updated correctly.
when try compile 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 type'structitem_t *' 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 *' 'item_t' expected return new_node; ^
item_t create_node(char *input)
should be
item_t *create_node(char *input)
what return structure should returning pointer of type struct item
.
Comments
Post a Comment