You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++链表追加PatientList节点问题:输出出现多余NULLPTR

问题排查与修复:PatientList链表输出多余NULLPTR

问题描述

实现PatientList类的append()成员函数,功能是将传入的节点追加到链表末尾,但运行后输出出现多余的NULLPTR,不符合预期:

  • 当前输出:
Patients : name1 name2 name3 name4 NULLPTR name5
  • 期望输出:
Patients : name1 name2 name3 name4 name5

问题根源分析

  1. PrintList函数逻辑错误:
    代码中只要当前节点的_next为nullptr就输出NULLPTR,这个逻辑完全错误——它会在每个末尾节点(包括链表的最后一个节点)前输出该标记,而实际需求不需要这个调试内容。

  2. append函数存在冗余代码:
    创建了newNode但未将其加入链表,既造成内存泄漏,也属于无效代码;不过这不是输出异常的直接原因,但需要清理。

修复方案

1. 修复PrintList函数

删除输出NULLPTR的代码块,只保留遍历输出节点名称的核心逻辑:

void PatientList::PrintList() {
    PatientList *temp = this;
    std::cout << "Patients : ";
    while (temp) {
        std::cout << temp->_content->get_name() << " ";
        temp = temp->_next;
    }
    std::cout << std::endl;
}

2. 修复append函数

删除无用的newNode相关代码,简化逻辑,确保正确找到链表末尾并挂载传入节点:

void PatientList::append(PatientList *node) {
    PatientList *temp = this;
    // 找到链表最后一个节点
    while (temp->_next != nullptr) {
        temp = temp->_next;
    }
    // 将传入节点挂载到最后一个节点的_next
    temp->_next = node;
    // 双向链表逻辑:设置传入节点的前驱指针
    node->_prev = temp;
}

3. 补充默认构造函数初始化(可选)

为避免未初始化成员导致的潜在问题,修改PatientList的默认构造函数,初始化所有指针成员:

PatientList() : _content(nullptr), _next(nullptr), _prev(nullptr) {}

完整修复后代码

#include <iostream>
#include <string>

class Patient {
   public:
    Patient();
    Patient(std::string name);
    ~Patient();
    std::string get_name(void);

   private:
    std::string _name;

   protected:
};

Patient::Patient() : _name("Patient") {}

Patient::Patient(std::string name) : _name(name) {}

Patient::~Patient() {}

std::string Patient::get_name(void) { return _name; }

class PatientList {
private:
    Patient *_content;
    PatientList *_next;
    PatientList *_prev;

public:
    PatientList();
    PatientList(Patient *patient);
    ~PatientList();
    bool isEnd(void);
    void append(PatientList *node);
    Patient *getContent();
    PatientList *getNext();
    void setNext(PatientList *next);
    void setContent(Patient *content);
    void PrintList();
};

PatientList::PatientList() : _content(nullptr), _next(nullptr), _prev(nullptr) {}
PatientList::PatientList(Patient *content)
    : _content(content), _next(nullptr), _prev(nullptr) {}
PatientList::~PatientList() {}
bool PatientList::isEnd() {
    return _next == nullptr;
}
Patient *PatientList::getContent() { return _content; }
PatientList *PatientList::getNext() { return _next; }
void PatientList::setNext(PatientList *next) { this->_next = next; }
void PatientList::setContent(Patient *content) { this->_content = content; }

// 修复后的PrintList函数
void PatientList::PrintList() {
    PatientList *temp = this;
    std::cout << "Patients : ";
    while (temp) {
        std::cout << temp->_content->get_name() << " ";
        temp = temp->_next;
    }
    std::cout << std::endl;
}

// 修复后的append函数
void PatientList::append(PatientList *node) {
    PatientList *temp = this;
    while (temp->_next != nullptr) {
        temp = temp->_next;
    }
    temp->_next = node;
    node->_prev = temp;
}

int main() {
    Patient k1("name1");
    Patient k2("name2");
    Patient k3("name3");
    Patient k4("name4");
    Patient k5("name5");

    PatientList P1(&k1);
    PatientList P2(&k2);
    PatientList P3(&k3);
    PatientList P4(&k4);
    PatientList P5(&k5);

    P1.append(&P2);
    P1.append(&P3);
    P1.append(&P4);
    P1.append(&P5);

    P1.PrintList();
}

测试结果

运行修复后的代码,输出与期望一致:

Patients : name1 name2 name3 name4 name5 

内容的提问来源于stack exchange,提问作者Just_a_Beginner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:40:37