In C/C++, if only variable definitions are used, is it Nil?

Asked 1 years ago, Updated 1 years ago, 500 views

We are currently running ALDS1_8_A.It's a two-minute search tree.

The answer codes are as follows:

In the insert function, x is the current comparison node, y is the parent node, and z is inserted. I think you are thinking as , but I don't know the behavior of putting the root pointer of the initial value for x.
Specifically, the conditional statement of the While statement.
I don't think x is Nil, but does x contain Nil from the beginning?
I think the initial value in x is root instead of Nil.
I think root is a Node pointer type, but root is not Nil.
In C/C++, if a variable is defined but no value is actually in it, is it Nil?

I look forward to your kind cooperation.

structure Node {
  int key;
  Node* right, *left;
};

Node* root, *NIL;

void insert(intk){
  Node*y = NIL;
  Node* x = root;
  Node*z;

  z=(Node*)malloc(sizeof(Node));
  z->key=k;
  z->left=NIL;
  z->right=NIL;

  while(x!=NIL){
    y = x;
    if(z->key<x->key)
      x = x - > left;
    else
      x = x - > right;
  }

  if(y==NIL){
    root=z;
  } else{
    if(z->key<y->key)
      y->left=z;
    else
      y->right=z;
  }
}

c++ c

2023-01-09 09:33

1 Answers

Node*root,*NIL;

root and NIL are global variables, so the initial value is 0.Therefore, both the local variable x and y have initial values of 0. The comparison with NIL is compared to 0.

For proper code in C language, it is common to use NULL instead of creating a variable called NIL.For proper C++ code, it is normal to use nullptr instead of creating a variable called NIL.


2023-01-09 09:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.