简体   繁体   中英

in C++ ,what is this for? Node* &head,

I have a question regarding this code piece. Node* &head , in insert() function, I have hard time understanding meaning of it. Is this "the reference of a pointer poniting to Node structure" ?

But why in here getDepth(Node *head) he uses Node *head ? Is this a good practice? what's the better way to write it if possible?

Thank you!

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

const int maxn = 100;
struct Node{
    int key;
    Node *lchild, *rchild, *parent;
};
Node *head, *p, node[maxn];
int cnt;

void init(){
    head = p = NULL;
    memset(node, '\0', sizeof(node));
    cnt = 0;
}
void insert(Node* &head, int x){
    if(head == NULL){
        node[cnt].key = x;
        node[cnt].parent = p;
        head = &node[cnt++];
        return;
    }
    p = head;
    if(x < head->key)
        insert(head->lchild, x);
    else
        insert(head->rchild, x);
}
int d = 0, num = 0, dep[maxn];
void getDepth(Node *head){
    if(head == NULL) return;
    ++d;
    getDepth(head->lchild);
    if(head->lchild == NULL && head->rchild == NULL)
        dep[num++] = d;
    getDepth(head->rchild);
    --d;
}
bool isBalance(Node *head){
    if(head == NULL) return true;
    getDepth(head);
    int max = dep[0], min = dep[0];
    for(int i=0; i<num; ++i){
        if(dep[i]>max) max = dep[i];
        if(dep[i]<min) min = dep[i];
    }
    if(max-min > 1) return false;
    else return true;
}
int main(){
    init();
    int a[] = {
        5, 3, 8, 1, 4, 7, 10, 2, 6, 9, 11, 12
    };
    for(int i=0; i<12; ++i)
        insert(head, a[i]);
    cout<<isBalance(head)<<endl;
    return 0;
}

If you pass Node *head instead of Node* &head , and if "head" is changed, the change will not be available in the main function.

If you want these changes to be available, either you return head or pass Node* &head or Node **head .

In getDepth() there is no need to change the "head" pointer. So passing Node *head works fine.

That's exactly what it is: a reference to a pointer to a Node . It allows you to pass in a Node* by reference, so that when you modify the pointer inside the function, the modification will be seen outside.

A simple example involving a reference parameter:

void inc(int& x) {
  x++;
}

If I have a variable int y = 0; , pass it with inc(y) , and then check the value of y afterwards, it will have value 1 . If the parameter had not been a reference type, the value would be 0 , because only the copy inside inc would have been modified.

Your case is just the same. You pass the Node* by reference so that you can modify the pointer and have it visible on the outside.

This construction means reference to a variable that has type Node *. The problem is that if you simply declare that parameter of the function insert as Node * then inside the function this parameter bahaves as a local variable. So any changes of it will be lost after exiting the function. So in this case as the result original variable head will not be changed. Passing the pointer by reference guarantees that any changes of the head inside the function will be kept in the original variable.

The person who wrote this code is a jerk, and wrote unreadible code. Simply terrible.

However, Node* &head is a reference to a pointer to a node. So basically it passes the pointer to the the head by reference, so you can change the value of the head ptr.

For example:

Node *A = NULL;
insert(A,10);
//now A has changed, A != NULL;

You are looking at a reference to a pointer.

http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

I would avoid using references since they are indistinguishable from passing something by value when calling it.

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