简体   繁体   English

链接列表的删除和复制

[英]Linked list deletion and duplication

In the code I copied newnode to the head node and also to the temp node. 在代码我复制newnodehead节点,并且还向temp节点。 But when I delete an instance of data, it seems to affect the other locations as well. 但是,当我删除数据实例时,它似乎也会影响其他位置。 When I freed newnode it also erases the content of head and temp .How is this happening? 当我释放newnode它也会删除headtemp的内容。这是怎么回事?

Though I copied data initially, the data is freed. 尽管我最初复制了数据,但数据已释放。 This is due to dereferencing? 这是由于取消引用? So what should I do if I want to have a copy list and want to manipulate this without affecting the original? 那么,如果我想拥有一个副本列表并希望在不影响原始副本的情况下进行操作,该怎么办?

And I initially malloc'd the memory I want by malloc() but in later copy operations I see at codes they are not malloc() 'ed rather just copied. 我最初通过malloc()分配了我想要的malloc()但是在以后的复制操作中,我在代码中看到它们不是malloc()的版本,而只是被复制了。 And how is it still it working? 仍然如何运作? Do my two questions have a relation? 我的两个问题有关系吗?

#include <iostream>
#include <cstdlib>
using namespace std;   

struct node{
    int data;
    struct node*next;
};

int main()
{
    struct node*newnode=(struct node*)malloc(sizeof(struct node));
    newnode->data=2;
    newnode->next=NULL;
    struct node*head=NULL;
    head=newnode;
    struct node*temp=newnode;

    while(head!=NULL)
    {
        cout<<head->data;
        head=head->next;
    }

    cout<<temp->data;
    free(newnode);
    free(head);
    cout<<temp->data;
    return 0;
}

With struct node *newnode=(struct node*)malloc(sizeof(struct node)); 使用struct node *newnode=(struct node*)malloc(sizeof(struct node)); you allocate a piece of memory for a node once and then you assign the address of this memory to all other node pointers. 您一次为一个节点分配一块内存,然后将该内存的地址分配给所有其他节点指针。 So when you free this piece of memory, the node isn't available any more to any of the node pointers. 因此,当您释放该内存时,该节点将不再可用于任何节点指针。

struct node *head=newnode;    // head now points to *newnode
struct node *temp=newnode;    // temp now also points to *newnode
...
free(newnode);    // newnode, head and temp point to released memory now
free(head);       // oops! head was released already by the previous statement

Note: this is the C explanation. 注意:这是C的解释。 In C++ the constructor of a class can do the memory allocation and a redefined assignment operator can create a new instance of the object (but I am not a C++ programmer). 在C ++中,类的构造函数可以执行内存分配,而重新定义的赋值运算符可以创建对象的新实例(但我不是C ++程序员)。

The following function creates a copy of the list: 以下函数创建列表的副本:

struct node *copylist(struct node *oldlist)
{
    struct node *newhead, *list;
    if (!oldlist) return(0);
    list= newhead= malloc(sizeof(struct node));
    *newhead= *oldlist;
    while (oldlist->next) {
        list->next= malloc(sizeof(struct node));
        oldlist= oldlist->next;
        list= list->next;
        *list= *oldlist;
    }
    list->next= NULL;
    return(newhead);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM