无法在函数调用之外更改节点参数
Can't change node parameters outside of function call
我正在创建一个函数,试图将 int 插入到树中的节点中。据我所知,我的插入功能运行良好,但在函数之外,就像它从未发生过一样。这是我的代码:
LCRS.H:
using namespace std;
#include <cstdlib>
#include <iostream>
class node{
public:
int data;
node *right;
node *below;
node()
{
right = NULL;
below = NULL;
}
};
class lcrs{
public:
node *root;
bool search(int, node*);
void print(node*);
void insert(int, node*);
int getHeight(node*);
lcrs()
{
root = NULL;
}
};
和 lcrs.cpp:
using namespace std;
#include "lcrs.h"
bool lcrs::search(int x, node *b)
{
if(b == NULL)
return false;
else
{
if(b->data == x)
return true;
else
{
return search(x, b->right) || search(x, b->below);
}
}
}
void lcrs::print(node *z)
{
if(z->right == NULL && z->below == NULL)
{
cout << z->data << endl;
}
else if(z->below == NULL && z->right != NULL)
{
cout << z->data << ", ";
print(z->right);
}
}
else if(z->below != NULL && z->right == NULL)
{
cout << z->data << ", ";
print(z->below);
}
else
{
print(z->right);
print(z->below);
}
}
void lcrs::insert(int x, node *a)
{
if(a == NULL)
{
node *newnode;
newnode = new node;
newnode->data = x;
a = newnode;
cout << a->data << endl;
}
else if(a->data < x)
{
if(a->right != NULL)
{
insert(x, a->right);
}
else
a->right->data = x;
}
else
{
if(a->below != NULL)
{
insert(x, a->below);
}
else
{
a->below->data = x;
}
}
}
int lcrs::getHeight(node *h)
{
int height = 0;
if(h->below != NULL)
{
height ++;
return getHeight(h->below);
}
else
return height;
}
最后是我的主要.cpp:
using namespace std;
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "lcrs.h"
int main()
{
char *temp1;
char *temp2;
temp1 = new char;
temp2 = new char;
lcrs tree;
do{
cout << "LCRS> ";
cin >> temp1;
if(strcmp(temp1, "quit") == 0)
{
return 0;
}
if(strcmp(temp1, "insert") == 0)
{ cin >> temp2;
bool error;
for(int i=0; i<strlen(temp2)-1; i++)
{
if(!isdigit(temp2[i]))
{
cout << "Error!" << endl;
error = true;
}
}
if(!error)
{
tree.insert(atoi(temp2), tree.root);
if(tree.root == NULL)
cout << "Root is null." << endl;
else
cout << tree.root->data << endl;
}
}
else if(strcmp(temp1, "height") == 0)
{
if(tree.root == NULL)
cout << "-1" << endl;
else
cout << tree.getHeight(tree.root);
}
else if(strcmp(temp1, "preorder") == 0)
{
tree.print(tree.root);
}
}while(strcmp(temp1, "quit") !=0);
return 0;
}
所以我明白我的插入函数实际上并没有改变树的根,我只是不明白为什么。
非常感谢您的帮助。
您的代码:
void lcrs::insert(int x, node *a)
{
if(a == NULL)
{
node *newnode;
newnode = new node;
newnode->data = x;
a = newnode;
↑ 此赋值不会更新实际参数,因为它是按值传递的。
cout << a->data << endl;
}
else if(a->data < x)
{
if(a->right != NULL)
{
insert(x, a->right);
}
else
a->right->data = x;
↑ 在这里你知道(已经确保)a->right
是一个空指针。取消引用该空指针会产生未定义的行为。例如崩溃。
}
else
{
if(a->below != NULL)
{
insert(x, a->below);
}
else
{
a->below->data = x;
}
↑ 在这里你知道(已确保)a->below
是一个空指针。取消引用该空指针会产生未定义的行为。例如崩溃。 } }
作为一般性评论,below
和right
混合了两个隐喻。最好称它们为below
和above
,或者left
和right
。后一种选择非常普遍。
与其通过引用传递节点指针(以解决非更新问题),请考虑将其作为函数结果返回。
你正在将指针传递给一个 using pass by 值;要通过引用传递,你需要使用 void lcrs::insert(int x, node * & a)
它将引用传递给a
而不是a
本身。 发生的情况是,指向节点的指针被复制到局部变量中,在函数中引用为a
。 然后,该函数可以自由地对副本(甚至它指向的内存)进行更改,但是当函数返回时,调用方不知道副本发生了什么,因为它仍在查看原始副本。
通过引用传递,调用方和被调用函数都使用相同的东西,而不是一个使用副本,另一个使用原始函数。
相关文章:
- 节点插件 API 将数组作为函数参数传递
- 无法使用类型为"结构节点 *"的左值初始化类型为"结构节点 *"的参数
- 在没有参数列表的情况下无效使用模板名称"节点"
- C 指针节点帮助 - 将参数复制到链接的列表节点更改该节点的不同部分
- 用一个参数计数圆形链接列表中的节点
- 我不知道如何调用参数为节点类型的函数
- 从GCC功能树节点中检索函数参数
- 无法将参数从"this"转换为节点<T>*
- 无法在函数调用之外更改节点参数
- 节点C++插件 - 当类型数组(Float32Array)作为参数传递时,如何访问它?
- C++数据流编程:使用不同的模板参数互连节点
- "节点":使用类模板需要模板参数列表
- 序遍历的二叉搜索树,没有节点指针作为参数
- 将树的每个节点替换为节点左侧所有值的和,而不使用额外的整数指针参数
- c++如何从具有多个参数的节点中检索信息
- 面试编码-将指向节点结构的指针作为参数,并返回传入数据结构的完整副本
- 为什么我得到错误,模板参数需要'结构节点'
- 检查传递给节点c++插件的参数是否为Infinity或NaN
- C++参数 1 没有从"节点*"到"节点*&"的已知转换
- 使用c++引用参数在链表中分配新节点