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

C++姓名链表实现逻辑错误:仅显示单个节点求助

单链表仅显示一条数据的问题排查与修复

问题场景

开发维护姓名及对应含义的数据集项目,通过文件读取数据并插入单链表,调用display()时仅显示文本文件中的第一条数据。

原代码

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

class Node{
    public:
        string NAME;
        string MEANING;
        char INITIAL;
        Node *link;
        
        Node(){
        }
};

class LinkedList{
    private:
        Node *head, *tail, *current, *temp;
    public:
        LinkedList(){
            head=NULL;
        }
        
        void InsertAtTail(char initial, string name, string meaning){
            if(head==NULL){
                head = new Node;
                head->INITIAL = initial;
                head->NAME = name;
                head->MEANING = meaning;
                head->link = NULL;              
            }
            else{
                temp = new Node;
                temp->INITIAL = initial;
                temp->NAME = name;
                temp->MEANING = meaning;
                temp->link = NULL;
                tail->link = temp;
                tail = temp;
            }
        }
        
        void display(){
            Node *current = head;
            while(current!=NULL){
                cout<<"Initial: "<<current->INITIAL;
                cout<<"\tName: "<<current->NAME;
                cout<<"\tMeaning: "<<current->MEANING<<endl;
                current = current->link;
            }
            
        }
};

int main(){
    LinkedList list;
    fstream boys( "boys.txt", ios::in );
    string name, meaning;
    char initial;
    while(boys >> name >> meaning >> initial)
    {
        list.InsertAtTail (initial, name, meaning);
    }
    list.display();
    boys.close();
    return 0;
}

问题分析

  1. tail指针未初始化:LinkedList构造函数仅初始化了head=NULL,但tail作为类成员变量未被赋值,初始为野指针。
  2. 首节点插入后未绑定tail:当插入第一个节点(head==NULL分支)时,仅创建了head节点,但未将tail指向head,导致后续插入节点时,tail->link访问的是野指针,程序行为异常(仅保留第一个节点,后续节点无法正确插入链表)。

修复方案

对LinkedList类做两处修改:

  1. 构造函数中初始化tail为NULL;
  2. 首节点插入完成后,将tail指向head,确保后续插入操作能正确找到链表尾部。

修复后的代码

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

class Node{
    public:
        string NAME;
        string MEANING;
        char INITIAL;
        Node *link;
        
        Node(){
        }
};

class LinkedList{
    private:
        Node *head, *tail, *current, *temp;
    public:
        LinkedList(){
            head=NULL;
            tail=NULL; // 初始化tail为NULL
        }
        
        void InsertAtTail(char initial, string name, string meaning){
            if(head==NULL){
                head = new Node;
                head->INITIAL = initial;
                head->NAME = name;
                head->MEANING = meaning;
                head->link = NULL;
                tail = head; // 首节点插入后,tail指向head
            }
            else{
                temp = new Node;
                temp->INITIAL = initial;
                temp->NAME = name;
                temp->MEANING = meaning;
                temp->link = NULL;
                tail->link = temp;
                tail = temp;
            }
        }
        
        void display(){
            Node *current = head;
            while(current!=NULL){
                cout<<"Initial: "<<current->INITIAL;
                cout<<"\tName: "<<current->NAME;
                cout<<"\tMeaning: "<<current->MEANING<<endl;
                current = current->link;
            }
            
        }
};

int main(){
    LinkedList list;
    fstream boys( "boys.txt", ios::in );
    string name, meaning;
    char initial;
    while(boys >> name >> meaning >> initial)
    {
        list.InsertAtTail (initial, name, meaning);
    }
    list.display();
    boys.close();
    return 0;
}

额外验证点

确保boys.txt中的数据格式与读取逻辑匹配:每行需为姓名 含义 首字母,且含义不含空格(若含义有空格,需修改读取方式,比如用getline配合分隔符处理)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 22:50:21