简体   繁体   中英

Inserting into a tree using a void function

So I have a global variable called

struct * tree root = NULL;

and I initialize it inside a function called void init() .

The problem here, is that for my assignment, I have been given void insert() for the ADT, and am forced to use this. But every time I to create a new node, I have no idea how to store this value, since I cannot return a node after the function is finished.

You have been given an insert function that is supposed to return void (aka no return value), but does it also mean you it cannot accept arguments? Not as you describe it. A function without arguments in C has a variable number of arguments. If your assigned declaration is void insert(void) then it's only solvable using globals (the new value to be inserted is in a global var 'newval') and a recursively called do_insert that accepts a value and the current node.

Don't know if this is in the rules, but if you delcare:

static struct * tree root;

then it'll be visible to all functions in the source file.

Can you give some more details on the structure of the tree node? Like are there left and right pointers? Are there other fields we should know about?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM