链表的删除和复制 [英] Linked list deletion and duplication
问题描述
在code我复制 newnode
到头
节点,也给温度
节点。但是,当我删除数据的一个实例,它似乎影响其它位置为好。当我释放 newnode
也删除头的内容
和温度
。怎么会这样呢?
虽然我最初复制的数据,该数据被释放。这是由于解引用?所以,我应该怎么做,如果我想有一份名单,并希望操纵这种在不影响原有的?
和我最初是由 malloc分配我想要的内存的malloc()
但在以后的复制操作我在看codeS他们不是的malloc()
'ED,而只是复制。它是怎样还是工作?做我的两个问题有关系?
的#include<&iostream的GT;
#包括LT&; cstdlib>
使用命名空间std;结构节点{
int数据;
结构节点*接下来的;
};诠释的main()
{
结构节点* newnode =(结构节点*)malloc的(的sizeof(结构节点));
newnode->数据= 2;
newnode->接着= NULL;
结构节点*头= NULL;
头= newnode;
结构节点* TEMP = newnode; 而(头!= NULL)
{
COUT<<流浆>数据;
头=流浆>接下来,
} COUT<< TEMP-GT&;数据;
免费(newnode);
免费(头);
COUT<< TEMP-GT&;数据;
返回0;
}
通过结构节点* newnode =(结构节点*)malloc的(的sizeof(结构节点));
你分配一块内存的节点一次,然后你这个内存的地址分配给所有其他节点的指针。所以,当你释放这块内存,节点不提供任何更多的节点指针。
结构节点*头= newnode; //头部现在指向* newnode
结构节点* TEMP = newnode; //临时现在还指出,* newnode
...
免费(newnode); // newnode,头部和临时点公布的记忆现在
免费(头); //糟糕!头发布已经由previous声明
请注意:这是C解释。在C ++类的构造函数可以做内存分配和重新定义的赋值运算符可以创建对象的新实例(但我不是一个C ++程序员)。
下面的函数创建列表的副本:
结构节点* copylist(结构节点* oldlist)
{
结构节点* newhead,*名单;
如果回报(0)(oldlist!);
清单= = newhead的malloc(sizeof的(结构节点));
* newhead = * oldlist;
而(oldlist->下面){
列表 - >接下来=的malloc(sizeof的(结构节点));
oldlist = oldlist->接下来,
列表=列表 - >接下来,
*名单= * oldlist;
}
列表 - >接着= NULL;
返回(newhead);
}
In the code I copied newnode
to the head
node and also to the temp
node. 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?
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. 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));
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. 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).
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);
}
这篇关于链表的删除和复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!