链表深拷贝c++

发布于 2024-12-18 14:59:55 字数 1224 浏览 4 评论 0原文

我正在尝试为我制作的简单链接列表制作深层副本。我正在尝试了解它的基础知识,任何帮助将不胜感激。我只想获取旧列表中的第一个值并将其深度复制到新列表中。

#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 技术交流群。

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

发布评论

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

评论(1

逆光飞翔i 2024-12-25 14:59:55

您正在将 NULL 的 head_new 传递给 deepcopy。然后你尝试尊重(访问)它。这会给您带来分段错误(错误),因为您无法引用 NULL 指针。 (您无法访问任何内容,因为您的指针指向任何内容。)

要更正您的代码,必须为 main 中的 head_new 以及 deepcopy 中的每个下一个节点分配内存。此外,您还应该继续您的 new_linked_list,因为您将所有时间分配给同一个节点。

You are passing head_new which is NULL to deepcopy. 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 in main and for each next node in in deepcopy. Also you should move on your new_linked_list, as you assign all the time to the same node.

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