简体   繁体   English

C ++链表-取消分配节点会出现错误

[英]C++ Linked list - Deallocating node gives error

I created a linked list. 我创建了一个链表。 Insertion and traversing works fine. 插入和遍历效果很好。 Given below is the part in which I am deallocating the nodes. 下面给出的是我正在分配节点的部分。 I have three pointers now, nxt, start all of which are of the datatype node (the structure) now, nxt, start有三个指针now, nxt, start所有这些指针都是数据类型node (the structure)

now=start;
for(int i=0;i<n;i++){
    nxt=now->link;
    delete now->link;
    now=nxt;
}

start stores the address of the first node, now stores the address of the current node under process, nxt stores the address of the next node which is accessed from the link part of the node at address now . start存储第一节点的地址, now存储在处理中的当前节点的地址, nxt存储从所述节点的链路部分,在地址访问下一个节点的地址now

When I try to execute the program everything until deletion works properly and when it reaches deletion the program crashes and gives "Title : Stopped working" error. 当我尝试执行程序时,所有操作直到删除正常进行为止,当删除完成时,程序崩溃,并显示“标题:已停止工作”错误。 What is wrong with the code? 代码有什么问题?

I guess that's the problem: 我想这就是问题所在:

delete now;
now=nxt;

You delete something and then perform an assignment to it. 您删除某些内容,然后对其进行分配。

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

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