运算符->在 C++ 中无法按预期工作;

发布于 2024-12-24 19:13:13 字数 1453 浏览 0 评论 0原文

我在练习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 未正确传递到该方法中。

但这是有效的:

  1. Node* tail=head->append(2)->append(3)->append(4)->append(5); tail->append(head->next);
  2. 或者在两个方法中将 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:

  1. Node* tail=head->append(2)->append(3)->append(4)->append(5);
    tail->append(head->next);
  2. Or after I change return c->next to return 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戴着白色围巾的女孩 2024-12-31 19:13:13

您正在经历未定义的行为

问题是您期望 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 last append(). But that is not guaranteed.

溺渁∝ 2024-12-31 19:13:13

当您传递 head->next 时 - 在使用 head->append 更改它之前。恐怕您混淆了写入顺序和执行顺序。

在这种情况下,您将更改该值并在同一执行语句中读取它,这是未定义的行为。

When you're passing head->next - its before changing it with head->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.

长伴 2024-12-31 19:13:13

首先评估head->next。编译器可以自由地这样做;请参阅此问题

head->next is evaluated first. The compiler is at liberty to do so; see this question.

尤怨 2024-12-31 19:13:13

在评估该语句时,head->next 为 NULL(不指向任何内容)。

head->next is NULL (doesn't point to anything) at the time that statement is evaluated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文