什么是Node *&阳极? [英] What is Node *& aNode?

查看:171
本文介绍了什么是Node *&阳极?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:

  void insert(Node *& aNode,int x){
if (!aNode){
aNode = new Node(x);
aNode-> next = aNode;
return;
}

Node * p = aNode;
Node * prev = NULL;
do {
prev = p;
p = p-> next;
if(x< = p-> data&& x> = prev-> data)break; //对于情况1)
if((prev-> data> p-> data)&&(x -p- data || x> prev-> )break; // For case 2)
} while(p!= aNode); //当回到起点,然后停止。对于情况3)

Node * newNode = new Node(x);
newNode-> next = p;
prev-> next = newNode;
}

什么是Node *& aNode?

我应该如何使用这个函数,我的意思是,我应该传递哪种类型的参数?

解决方案

我认为这段代码是C ++,而不是C, Node *& aNode 是一个指向 Node ,所以你会传递一个 Node * 到函数,函数将引用它 Node * 指向可以更改。)



您可以找到关于引用的维基百科文章(C ++)有趣。



一个简单的例子:

  #include< iostream> 
void addOneToValue(int num){
++ num;
}

void addOneToRef(int& num){
++ num;
}

int main(){
int num =

// print 0
std :: cout<< num<< std :: endl;

//再次打印0(addOneToValue()没有效果)
addOneToValue(num);
std :: cout<< num<< std :: endl;

//打印1(addOneToRef()更改num的值)
addOneToRef(num);
std :: cout<< num<< std :: endl;
}

@ crashmstr的注释提醒我应该说他们与指针有什么不同。 维基百科有更好的工作,我可以尽管: / p>


  • 定义后不可能直接引用引用对象;

  • 一旦创建了引用,它以后就不能再引用另一个对象了。它不能重复。这通常是用指针完成的。

  • 引用不能为null,而指针可以;每个引用指的是一些对象,尽管它可能或可能不是有效的。

  • 引用不能未初始化。由于无法重新初始化引用,因此必须在创建引用后立即对其进行初始化。特别是,局部和全局变量必须在定义它们的地方进行初始化,而类实例的数据成员引用必须在类构造函数的初始化器列表中初始化。

  • 大多数编译器将支持一个null引用,没有太多的投诉,只有当你尝试以某种方式使用引用时崩溃。


In the following code:

void insert(Node *& aNode, int x) {
  if (!aNode) {
    aNode = new Node(x);
    aNode->next = aNode;
    return;
  }

  Node *p = aNode;
  Node *prev = NULL;
  do {
    prev = p;
    p = p->next;
    if (x <= p->data && x >= prev->data) break;   // For case 1)
    if ((prev->data > p->data) && (x < p->data || x > prev->data)) break; // For case 2)
  } while (p != aNode);   // when back to starting point, then stop. For case 3)

  Node *newNode = new Node(x);
  newNode->next = p;
  prev->next = newNode;
}

What is Node *& aNode?

How should I use this function, I mean, which type of parameter should I pass?

解决方案

I think this code is C++, not C, and Node *&aNode is a reference to a pointer to a Node, so you would pass a Node* to the function, and function would make a reference to that (so the memory location your Node* is pointing to can change).

You may find the Wikipedia article on References (C++) interesting.

A simple example:

#include <iostream>
void addOneToValue(int num) {
    ++num;
}

void addOneToRef(int &num) {
   ++num;
}

int main() {
   int num = 0;

   // print 0
   std::cout << num << std::endl;

   // print 0 again (addOneToValue() has no effect)
   addOneToValue(num);
   std::cout << num << std::endl;

   // print 1 (addOneToRef() changes the value of num)
   addOneToRef(num);
   std::cout << num << std::endl;
}

@crashmstr's comment reminded me that I should say how they're different from pointers. Wikipedia does a better job that I could though:

  • It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.
  • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
  • References cannot be null, whereas pointers can; every reference refers to some object, although it may or may not be valid.
  • References cannot be uninitialized. Because it is impossible to reinitialize a reference, they must be initialized as soon as they are created. In particular, local and global variables must be initialized where they are defined, and references which are data members of class instances must be initialized in the initializer list of the class's constructor.
  • Most compilers will support a null reference without much complaint, crashing only if you try to use the reference in some way.

这篇关于什么是Node *&amp;阳极?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆