c++ - 为什么这段代码不能用于删除链表中的节点?

标签 c++ pointers linked-list delete-operator

我一直在学习链表的工作原理,并开始用 C++ 构建一个实现来强化这些概念。一切顺利,直到我创建了一个删除所有节点的功能。我想出了一个解决方案(这是注释代码),但我似乎无法弄清楚为什么其他代码不起作用。

Node 对象是使用“new”创建的类的实例。因此,“删除”用于删除它。

我想这可能与删除对象和重用指针变量有关。然后我遇到了这个:What happens to a pointer itself after delete? 我已经盯着它看了一段时间,试图弄清楚它可能是什么,但我研究过的任何东西似乎都无法提供答案。

到目前为止,我不认为这与我的实现有关,因为当用解决方案代码替换代码时,程序按预期工作。

代码输出了每个地址,但似乎并没有真正删除对象。如果我在 Windows 中运行该程序,该程序实际上将锁定并且永远不会离开 while 循环。不是无限循环,它只是卡住了,函数永远不会完成。如果我在 C4Droid 上运行它,程序不会锁定,但函数退出后节点仍然存在。

所以我的问题是,为什么当前代码不起作用? (忽略注释代码。这是一个可行的解决方案。)是否有一些我忽略了指针变量的简单内容?先感谢您。

void LinkedList::deleteAll() {
Node *pCurrent = pHead;

while(pCurrent){
    Node *pNext = pCurrent->pNext;
    std::cout << pCurrent << std::endl;
    delete pCurrent;
    pCurrent = nullptr;
    pCurrent = pNext;

//  pHead = pHead->pNext;
//  delete pCurrent;
//  pCurrent = pHead;
}
}

节点类

class  Node{
    public:
        Node(string content):data(content){}
        string getData(){
            return data;
        }
        Node *pNext = nullptr;
    private:
        string data;
    };

链表.h

/*
 * LinkedList.h
 *
 *  Created on: Oct 3, 2015
 *      Author: Anthony
 */

#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_

#include<string>
using std::string;

class LinkedList {
public:
    LinkedList();
    virtual ~LinkedList();
    int length();
    void addNode(string nodeContent);
    void deleteNode(string nodeContent);
    void deleteAll();
private:
    class  Node{
    public:
        Node(string content):data(content){}
        string getData(){
            return data;
        }
        Node *pNext = nullptr;
    private:
        string data;
    };

    Node *pHead = nullptr;

};

#endif /* LINKEDLIST_H_ */

链表.cpp

    /*
 * LinkedList.cpp
 *
 *  Created on: Oct 3, 2015
 *      Author: Anthony
 */

#include "LinkedList.h"
#include <iostream>
LinkedList::LinkedList() {
    // TODO Auto-generated constructor stub

}

LinkedList::~LinkedList() {
    // TODO Auto-generated destructor stub
}

int LinkedList::length() {
    Node *current = pHead;
    int count = 0;
    while(current){
        count++;
        current = current->pNext;
    }
    return count;
}

void LinkedList::addNode(std::string nodeContent) {
    Node *newNode = new Node(nodeContent);
    newNode->pNext = pHead;
    pHead = newNode;
}

void LinkedList::deleteNode(std::string nodeContent) {
}

void LinkedList::deleteAll() {
    Node *pCurrent = pHead;

    while(pCurrent){
        Node *pNext = pCurrent->pNext;
        std::cout << pCurrent->pNext << std::endl;
        delete pCurrent;
        pCurrent = nullptr;
        pCurrent = pNext;

    //  pHead = pHead->pNext;
    //  delete pCurrent;
    //  pCurrent = pHead;
    }
}

主要.cpp

/*
 * main.cpp
 *
 *  Created on: Oct 3, 2015
 *      Author: Anthony
 */

#include<iostream>
#include "LinkedList.h"

int main(int argc, char **argv){

    using namespace std;

    LinkedList list = LinkedList();
    list.addNode(string("Test"));
    list.addNode(string("Test1"));

    list.deleteAll();
    cout << list.length() << endl;

    return 0;
}

最佳答案

假设(这是一个很大的假设)您的链表被正确地放在一起,那么注释代码有效而新代码无效的问题就相当简单了。

  pHead = pHead->pNext;
  delete pCurrent;
  pCurrent = pHead;

在上面的代码中,您在循环时将 pHead 指针移动到列表中。当循环结束时,pHead 指针为 nullptr,这是正确的,因为列表现在是空的。

Node *pNext = pCurrent->pNext;
    std::cout << pCurrent << std::endl;
    delete pCurrent;
    pCurrent = nullptr;
    pCurrent = pNext;

对于新的、未注释的代码,您没有在循环结束后设置 pHead 指针,从而使其指向垃圾。之后对链表的任何使用都将失效。

所以不是函数没有删除所有节点,而是在删除节点后,链表有一个野生的 pHead 指针,并使用链接list 的 pHead 节点在任何后续函数中变得不稳定。

尝试以下操作:

void LinkedList::deleteAll() {
Node *pCurrent = pHead;
while(pCurrent){
    Node *pNext = pCurrent->pNext;
    delete pCurrent;
    pCurrent = nullptr;
    pCurrent = pNext;
}
pHead = nullptr;  // Sets the head pointer to nullptr, denoting that the list is empty.

关于c++ - 为什么这段代码不能用于删除链表中的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928360/

相关文章:

c++ - 在函数中返回 2 个值

c++ - IID_MediaControl 未声明的标识符

c++ - 如何从指针中提取引用?

c++ - 将指针数组插入 std::vector 避免使用 push_back 复制对象

c - 删除节点,链表不起作用

c++ - 从 Slot 刷新 QML Listview

c# - 为 Node.js 应用程序创建 COM API 或类似的 API

ios - 解析指针和关系

c - 程序中的未知错误

java - java中队列的链表实现中的大小无法正确计算