链表深拷贝c++
我正在尝试为我制作的简单链接列表制作深层副本。我正在尝试了解它的基础知识,任何帮助将不胜感激。我只想获取旧列表中的第一个值并将其深度复制到新列表中。
#include<iostream>
using namespace std;
struct listrec
{
char value;
struct listrec *next;
};
void deepcopy(listrec *old_linked_list, listrec *new_linked_list)
{
while(old_linked_list != NULL)
{
new_linked_list->value = old_linked_list->value;
new_linked_list->next = old_linked_list->next;
old_linked_list = old_linked_list->next;
new_linked_list = new_linked_list->next;
}
}
int main()
{
listrec x1,x2,x3;
listrec *head_old, *head_new=NULL;
x1.value = 'a';
x1.next = &x2;
x2.value = 'c';
x2.next = &x3;
x3.value = 'w';
x3.next = NULL;
head_old = &x1;
head_new = head_old;
deepcopy(head_old, head_new);
//print old list
cout<<"Old List: "<<endl;
while(head_old != NULL)
{
cout<<head_old->value<<endl;
head_old= head_old->next;
}
cout<<endl;
//print copied list
cout<<"Copied list: "<<endl;
while(head_new != NULL)
{
cout<<head_new->value<<endl;
head_new= head_new->next;
}
system("pause");
return 0;
}
该程序可以运行并且会生成一个副本,但我只想确保它是深副本而不是浅副本。你们觉得怎么样?
I'm trying to make a deep copy for my simple linked list that I've made. I'm trying to get the basics of it and any help would be appreciated. I just want to take the first value in the old list and deep copy it to the new list.
#include<iostream>
using namespace std;
struct listrec
{
char value;
struct listrec *next;
};
void deepcopy(listrec *old_linked_list, listrec *new_linked_list)
{
while(old_linked_list != NULL)
{
new_linked_list->value = old_linked_list->value;
new_linked_list->next = old_linked_list->next;
old_linked_list = old_linked_list->next;
new_linked_list = new_linked_list->next;
}
}
int main()
{
listrec x1,x2,x3;
listrec *head_old, *head_new=NULL;
x1.value = 'a';
x1.next = &x2;
x2.value = 'c';
x2.next = &x3;
x3.value = 'w';
x3.next = NULL;
head_old = &x1;
head_new = head_old;
deepcopy(head_old, head_new);
//print old list
cout<<"Old List: "<<endl;
while(head_old != NULL)
{
cout<<head_old->value<<endl;
head_old= head_old->next;
}
cout<<endl;
//print copied list
cout<<"Copied list: "<<endl;
while(head_new != NULL)
{
cout<<head_new->value<<endl;
head_new= head_new->next;
}
system("pause");
return 0;
}
The program works and it makes a copy but I just want to make sure its a deep copy and not a shallow copy. What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将 NULL 的
head_new
传递给deepcopy
。然后你尝试尊重(访问)它。这会给您带来分段错误(错误),因为您无法引用 NULL 指针。 (您无法访问任何内容,因为您的指针指向任何内容。)要更正您的代码,必须为
main
中的head_new
以及deepcopy 中的每个下一个节点分配内存
。此外,您还应该继续您的new_linked_list
,因为您将所有时间分配给同一个节点。You are passing
head_new
which is NULL todeepcopy
. Then you try to deference (access) it. This gives you segmentation fault (error), since you cannot deference a NULL pointer. (You cannot access nothing, because your pointer points to nothing.)To correct your code must allocate memory for
head_new
inmain
and for each next node in indeepcopy
. Also you should move on yournew_linked_list
, as you assign all the time to the same node.