运算符->在 C++ 中无法按预期工作;
我在练习c++中的单链表(练习如何找到循环链表的起始节点),但发现运算符->的使用非常混乱。我正在使用 Visual studio 2010 C++ Express
这工作完美: head->append(2)->append(3)->append(4)->append(5)
但是这不起作用(创建循环链表): head->append(2)->append(3)->append(4)->append(5)->append(head->next)
当我跳跃时在此方法和调试中,似乎 head->next
未正确传递到该方法中。
但这是有效的:
- Node* tail=head->append(2)->append(3)->append(4)->append(5); tail->append(head->next);
- 或者在两个方法中将
return c->next
更改为return head
后,< code>head->append(2)->append(3)->append(4)->append(5)->append(head->next) 也有效。
我在这里缺少什么?谢谢你!
我的代码详细信息如下:
void main(){
Node* head=new Node(1);
Node* tail=head->append(2)->append(3)->append(4)->append(5)->append(head->next);
cin.get();
}
class Node{
public:
Node* next;
int data;
bool marked;
Node(int d){
data=d;
marked=false;
next=NULL;
}
Node* append(int d){
Node* c=this;
while(c->next!=NULL){
c=c->next;
}
c->next=new Node(d);
return c->next;
}
Node* append(Node* n){
Node* c=this;
while(c->next!=NULL){
c=c->next;
}
c->next=n;
return c->next;
}
};
I was practicing single linked list in c++ (practicing how to find the beginning node of the circular list), but found the use of operator -> very confusing. I'm using Visual studio 2010 C++ Express
This works perfectly: head->append(2)->append(3)->append(4)->append(5)
But this doesn't work (to create a circular linked list): head->append(2)->append(3)->append(4)->append(5)->append(head->next)
When I jump in this method and debug, it seems head->next
is not passed correctly into the method.
But this works:
Node* tail=head->append(2)->append(3)->append(4)->append(5);
tail->append(head->next);- Or after I change
return c->next
toreturn head
in the two methods,head->append(2)->append(3)->append(4)->append(5)->append(head->next)
also works.
What am I missing here? Thank you!
Details of my code is as follows:
void main(){
Node* head=new Node(1);
Node* tail=head->append(2)->append(3)->append(4)->append(5)->append(head->next);
cin.get();
}
class Node{
public:
Node* next;
int data;
bool marked;
Node(int d){
data=d;
marked=false;
next=NULL;
}
Node* append(int d){
Node* c=this;
while(c->next!=NULL){
c=c->next;
}
c->next=new Node(d);
return c->next;
}
Node* append(Node* n){
Node* c=this;
while(c->next!=NULL){
c=c->next;
}
c->next=n;
return c->next;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在经历未定义的行为。
问题是您期望
head->next
在特定时间(就在调用最后一个append()
之前)进行评估。但这并不能保证。You are experiencing undefined behavior.
The problem is that you are expecting
head->next
to be evaluated at a particular time (right before calling the lastappend()
. But that is not guaranteed.当您传递
head->next
时 - 在使用head->append
更改它之前。恐怕您混淆了写入顺序和执行顺序。在这种情况下,您将更改该值并在同一执行语句中读取它,这是未定义的行为。
When you're passing
head->next
- its before changing it withhead->append
. I'm afraid you're confusing the order of writing with the order of execution.In this case you're changing the value and reading it in the same execution statement, that's undefined behavior.
首先评估
head->next
。编译器可以自由地这样做;请参阅此问题。head->next
is evaluated first. The compiler is at liberty to do so; see this question.在评估该语句时,head->next 为 NULL(不指向任何内容)。
head->next is NULL (doesn't point to anything) at the time that statement is evaluated.