为什么我不能将新元素推送到列表 [英] Why can't I pushback a new element to a list
问题描述
我遇到一个问题,当我试图推回一个新的元素到列表。
这个问题是根据其调用编写的PushBack主体。所以我的问题是关于函数 PushBack()
。
include< iostream>
class Node {
public:
static Node * MakeNode(){return new Node(100); }
Node * prev;
Node * next;
int value;
private:
Node(int num):value(num),prev(NULL),next(NULL){}
};
void PushBack(Node * simple,Node * newNode){
if(simple == NULL){
//仍为空列表
simple = newNode;
}
else {
//需要循环到列表的末尾,因为类中没有tail
Node * itr = simple;
while(itr-> next!= NULL){
itr = itr-> next;
}
newNode-> prev = itr;
itr-> next = newNode;
}
return;
}
int main()
{
Node * simple = NULL;
PushBack(simple,Node :: MakeNode());
std :: cout<< (simple == NULL);
PushBack(simple,Node :: MakeNode());
PushBack(simple,Node :: MakeNode());
PushBack(simple,Node :: MakeNode());
while(simple!= NULL){
std :: cout< simple>值<< std :: endl;
simple = simple-> next;
}
return 0;
}
我对函数的参数类型有两个问题 PushBack()
:
-
/ code>是
static Node *
,但为什么我们需要在PushBack()中设置第二个参数类型
asNode *
?为什么不static Node *
? - 开始时,我尝试了
void PushBack(Node * simple,Node * newNode)
程序完成并退出PushBack()
,simple
变为NULL
。
因此,新的元素不能添加到列表中。我必须使用void PushBack(Node *& simple,Node * newNode)
为什么我需要在这里添加&
符号?我想如果我传递指针值到函数,指针简单可以直接更改。但是似乎不是这样的。
我的问题与这个区隔有什么区别:?
void changeVal(int * data){
for(int i = 0; i <9; i ++)
*(data + i)= 99;
}
void main()
{
int * data;
data = new int [10];
changeVal(data);
for(int i = 0; i <9; i ++)
std :: cout< data [i]< std :: endl;
}
我传递了指针 data
into changeVal()
,并且可以在函数内部更改内容。我不太明白两者之间的区别。
我没有在网上找到一些有用的解释,所以我在这里问。
<
MakeNode()
的返回类型为 static
,这样你可以调用该方法而不需要类的实例,你可以在类类型本身上调用该方法。在这种情况下, static
不是返回值的一部分,它是类方法本身的一部分。对于 PushBack()
,没有诸如 static
函数参数的东西。该函数对 Node
类的特定实例起作用,因此它只是一个常规指针。 简单
参数声明为 Node *
时,将传递
变量按值。该参数接收 Node *
变量的当前值的本地副本。函数读取或更改参数值的任何操作都是使用 copy 而不是原始变量完成的。这就是为什么 PushBack() c> main()
/ code>退出。
另一方面,当您将简单
参数声明为 Node *&
,您将通过引用传递 Node *
变量 。该参数接收原始 Node *
变量的内存地址。函数读取或更改参数值的任何操作都是使用原始变量而不是复制来完成的。这就是为什么当 PushBack()时, main()
code>退出。
参考本质上是一个编译器管理的指针。不允许将其设置为NULL,并且每当从/向其读取/写入值时,它都将自动解除引用。因此,如果你认为引用是一个隐式指针, PushBack(Node *&)
是等效于 PushBack / code>(带额外的编译器验证),类似于以下内容:
void PushBack Node * newNode){
if(* simple == NULL){
//仍然是一个空列表
* simple = newNode;
}
else {
//需要循环到列表的末尾,因为类中没有tail
Node * itr = * simple;
while(itr-> next!= NULL){
itr = itr-> next;
}
newNode-> prev = itr;
itr-> next = newNode;
}
return;
}
PushBack(& simple,...);
I met a problem when I try to pushback a new element to a list.
This problem is to write the function implementation of PushBack() according its calling in the main body. So my question is just about the function PushBack()
.
#include <iostream>
class Node{
public:
static Node* MakeNode() { return new Node(100); }
Node* prev;
Node* next;
int value;
private:
Node(int num) : value(num), prev(NULL), next(NULL) {}
};
void PushBack(Node* simple, Node* newNode){
if (simple == NULL){
//still a empty list
simple = newNode;
}
else{
//need to loop to the end of list because there is no "tail" in the class
Node* itr = simple;
while (itr->next != NULL){
itr = itr->next;
}
newNode->prev = itr;
itr->next = newNode;
}
return;
}
int main()
{
Node* simple = NULL;
PushBack(simple, Node::MakeNode());
std::cout << (simple == NULL);
PushBack(simple, Node::MakeNode());
PushBack(simple, Node::MakeNode());
PushBack(simple, Node::MakeNode());
while (simple != NULL){
std::cout << simple->value << std::endl;
simple = simple->next;
}
return 0;
}
I have two questions about the argument type of function PushBack()
:
the return type of
MakeNode()
isstatic Node*
, but why we need to set the second argument type inPushBack()
asNode*
? why notstatic Node *
?At beginning, I tried
void PushBack(Node* simple, Node* newNode)
but when the program finish and exit out fromPushBack()
,simple
becomesNULL
again. So the new elements cannot be added to the list. I must usevoid PushBack(Node*& simple, Node* newNode)
.
Why do I need to add a &
symbol here? I think if I pass the pointer value into the function, the pointer simple can be changed directly. But it seems this is not the case.
What is the difference between my problem and this segment:?
void changeVal(int* data){
for (int i = 0; i < 9; i++)
*(data + i) = 99;
}
void main()
{
int *data;
data = new int[10];
changeVal(data);
for (int i = 0; i < 9; i++)
std::cout << data[i] << std::endl;
}
I passed the pointer data
into changeVal()
, and the content can be changed inside the function. I don't quite understand the difference between the two.
I failed to find some useful explanation online so I am asking here.
No, the return type of
MakeNode()
isNode*
. You can declare a class method asstatic
so that you can call the method without needing an instance of the class beforehand, you call the method on the class type itself. Thestatic
in this case is not part of the return value, it is part of the class method itself. As forPushBack()
, there is no such thing as astatic
function parameter. The function is acting on a specific instance of theNode
class, so it is just a regular pointer.When you declare the
simple
parameter asNode*
, you are passing yourNode*
variable by value. The parameter receives a local copy of the current value of theNode*
variable. Anything the function does to read or alter the parameter value is done using that copy, not the original variable. This is why yoursimple
variable inmain()
was not changing whenPushBack()
exited.On the other hand, when you declare the
simple
parameter asNode*&
, you are passing yourNode*
variable by reference. The parameter receives the memory address of the originalNode*
variable. Anything the function does to read or alter the value of the parameter is done using the original variable directly, not a copy. This is why yoursimple
variable inmain()
was changing whenPushBack()
exited.
A reference is essentially a compiler-managed pointer. It is not allowed to ever be set to NULL, and it is auto-dereferenced whenever you read/write a value from/to it. So, if you think of a reference as an implicit pointer, PushBack(Node*&)
is functionality equivalent to PushBack(Node**)
(with extra compiler validation), similar to the following:
void PushBack(Node** simple, Node* newNode){
if (*simple == NULL){
//still a empty list
*simple = newNode;
}
else{
//need to loop to the end of list because there is no "tail" in the class
Node* itr = *simple;
while (itr->next != NULL){
itr = itr->next;
}
newNode->prev = itr;
itr->next = newNode;
}
return;
}
PushBack(&simple, ...);
这篇关于为什么我不能将新元素推送到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!