C++为数据结构中的 *next 分配新地址
有件事我脑子转不过来…… 基本上我得到了以下数据结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该是
当您执行
(**list).next = prev_temp;
时,您正在操作传递给方法的参数,而不是链表中的最后一个节点。should be
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.我假设 tail_return 应该采用 node_ll 的链接列表并删除 tail 元素?
是的,根据 @Aditya ,看起来该
行引起了问题。原因是您正在重新分配列表以指向倒数第二个元素(prev_temp)。
删除最后一个元素是正确完成的,
并且还删除了该行,
因为这也会删除您想要保留的倒数第二个元素。
另外,您当前正在返回倒数第二个元素。所以
改成
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
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
And also remove the line
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
to
太复杂了。如果您像这样编写它,
那么现在很清楚(呃)您更改了列表中的 first 元素。
这只是编写更清晰的代码的一个小建议。请参阅其他答案来解决您的问题。
is too complicated. If you write it like this
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).