C++为数据结构中的 *next 分配新地址

发布于 2025-01-06 13:47:10 字数 1123 浏览 1 评论 0原文

有件事我脑子转不过来…… 基本上我得到了以下数据结构:

struct node_ll {
    int payload;
    node_ll *next;  //pointer to next node
};

本质上是一堆数字。 我需要创建一个具有以下原型的方法:

int tail_return(node_ll **list)

其中 **list 是上述数据结构的内存地址。我的实现如下:

int tail_return(node_ll **list) {

    node_ll *temp;
    temp = *list;

    node_ll *prev_temp;
    prev_temp = *list;

    bool firstPass = true;

    while(temp){

        if(firstPass == true){
        temp = temp->next;
        firstPass = false;

        } else {
            temp = temp->next;
            prev_temp = prev_temp->next;

        }
    }

    int toReturn = prev_temp->payload;

    prev_temp->payload = 0;
    (**list).next = prev_temp;

    delete temp;
    delete prev_temp;
    return toReturn;
}

但是,我从测试运行中得到以下输出:

List a after head insertion of 2,4,6,8,10 elements:

{10,8,6,4,2}

now removing the last element

DELETED: 2

{10,0} where it's supposed to be: {10,8,6,4}

我做错了什么?显然,该方法找到了正确的删除值 - 2。但是为什么当我在删除后尝试打印它时,我最终得到 10 和 0?

there's something I can't get my head round...
Basically I'm given the following data structure:

struct node_ll {
    int payload;
    node_ll *next;  //pointer to next node
};

Which is essentially a stack of numbers.
I need to create a method with the following prototype:

int tail_return(node_ll **list)

where **list is the memory address of the above data structure. My implementation is as follows:

int tail_return(node_ll **list) {

    node_ll *temp;
    temp = *list;

    node_ll *prev_temp;
    prev_temp = *list;

    bool firstPass = true;

    while(temp){

        if(firstPass == true){
        temp = temp->next;
        firstPass = false;

        } else {
            temp = temp->next;
            prev_temp = prev_temp->next;

        }
    }

    int toReturn = prev_temp->payload;

    prev_temp->payload = 0;
    (**list).next = prev_temp;

    delete temp;
    delete prev_temp;
    return toReturn;
}

However I get the following output from test runs:

List a after head insertion of 2,4,6,8,10 elements:

{10,8,6,4,2}

now removing the last element

DELETED: 2

{10,0} where it's supposed to be: {10,8,6,4}

What am I doing wrong? Apparently the method finds the right value to delete - 2. But why when I try to print it after deletion I end up with 10 and 0?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

长途伴 2025-01-13 13:47:11
 (**list).next = prev_temp;

应该是

prev_temp->next = 0 ; 

当您执行 (**list).next = prev_temp; 时,您正在操作传递给方法的参数,而不是链表中的最后一个节点。

 (**list).next = prev_temp;

should be

prev_temp->next = 0 ; 

when you do (**list).next = prev_temp; you are manipulating the parameter which was passed to your method and not the last node in the linked list.

遗心遗梦遗幸福 2025-01-13 13:47:11

我假设 tail_return 应该采用 node_ll 的链接列表并删除 tail 元素?

是的,根据 @Aditya ,看起来该

(**list).next = prev_temp;

行引起了问题。原因是您正在重新分配列表以指向倒数第二个元素(prev_temp)。

删除最后一个元素是正确完成的,

delete temp;

并且还删除了该行,

delete prev_temp;

因为这也会删除您想要保留的倒数第二个元素。

另外,您当前正在返回倒数第二个元素。所以

int toReturn = prev_temp->payload;

改成

int toReturn = temp->payload;

I am assuming that tail_return is supposed to take a linked list of node_ll 's and delete the tail element?

Yes per @Aditya , looks like the

(**list).next = prev_temp;

line is causing a problem. The reason is that you are reassigning list to point to the second to last element (prev_temp).

Deleting the last element is correctly done by

delete temp;

And also remove the line

delete prev_temp;

since that removes the second to last element too, which you want to keep.

Plus you are currently returning the second to last element. So change

int toReturn = prev_temp->payload;

to

int toReturn = temp->payload;
格子衫的從容 2025-01-13 13:47:11
(**list).next = prev_temp;

太复杂了。如果您像这样编写它,

(*list)->next = prev_temp;

那么现在很清楚(呃)您更改了列表中的 first 元素。

这只是编写更清晰的代码的一个小建议。请参阅其他答案来解决您的问题。

(**list).next = prev_temp;

is too complicated. If you write it like this

(*list)->next = prev_temp;

it now becomes clear(er) that you change the first element in the list.

This is just a small recommendation for writing clearer code. See the other answers for the solutions to your problem(s).

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