In C, if I have a tree or linked list, I have to declare a function for insertion like
insert(Node ** node)
so I can insert a new root/head.
My question is, how to write it with references in C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You would write the function as
That is, the parameter is a reference to the
Node*variable holding the root of the tree. From there, you would proceed as usual, except that compared with the C version of the function you wouldn’t need to dereferencenodeto reassign the root For example, if you were to write the following in the initial (C) version of the code:You would just write
in the updated version of the code.
Hope this helps!