c++ - 从文本文件加载链接列表并打印时无限循环

标签 c++ printing linked-list

我在从文件读取到链表时遇到问题。我有一种(可能效率极低的方法)读取文件并将每个节点加载到链表中,但是如果我读取并尝试打印超过 1 行,那么我在打印时会出现无限循环。

加载代码

void readFile()
{
    string text;
    string temp; // Added this line
    node* newNode = new node;

    ifstream file;
    file.open("example.txt");

    for (int i = 0; i < 1; i++)
    {
        getline(file, temp);
        text = temp;

        string input = text;
        istringstream ss(input);
        string token;

        int counter = 0;

        while (getline(ss, token, ','))
        {
            cout << token << '\n';
            newNode->rented = token;
            counter++;

            if (counter == 0)
            {
                newNode->rented = token;
            }
            else if (counter == 1)
            {
                std::istringstream ss(token);
                ss >> newNode->maxload;
            }
            else if (counter == 2)
            {
                std::istringstream ss(token);
                ss >> newNode->passengers;
            }
            else if (counter == 3)
            {
                std::istringstream ss(token);
                ss >> newNode->doors;
            }
            else if (counter == 4)
            {
                newNode->registration = token;
            }
            else if (counter == 5)
            {
                std::istringstream ss(token);
                ss >> newNode->engine;
            }
            else if (counter == 6)
            {
                newNode->model = token;
            }
            else if (counter == 7)
            {
                newNode->make = token;
            }
        }
        system("pause");
        list.insertNode(newNode);
    }
    file.close();
}

插入节点

void linkedList::insertNode(node* newNode)
{
    newNode->nextNode = head;
    head = newNode;
}

打印代码

void linkedList::displayList()
{

    node* thisNode = head;

    if (head == NULL)
    {
        cout << "The list is empty\n";
        return;
    }
    else
        cout << "---------------------------------------------------------\n";
        cout << "\tMake\tReg Number\tRented\n";
        cout << "---------------------------------------------------------\n";
        cout << "\t";


    do
    {
        cout << setw(8) << left << thisNode->make;
        cout << setw(16) << left << thisNode->registration;
        cout << setw(10) << left << thisNode->rented;
        cout << "\n\t";
        thisNode = thisNode->nextNode;

    } while (thisNode != NULL);
    {
        cout << "\n\n";
    }
}

文本文件

car,Ferarri,12.0,aa,3,1,0,true
car,Mercedes,12.0,bb,5,4,0,false
car,Ford,1.6,cc,5,4,0,false

如果我将加载代码中的 for 循环设置为仅 1 次迭代,它会输出正确的显示:

0 aa true

但是,如果我将 for 循环设置为迭代超过 1 次,打印代码将无限打印最后一行读取的内容(忽略它之前的所有行)?

0 bb false
0 bb false
0 bb false
...

谁能看出这样做的任何原因?

最佳答案

您只是在分配一个节点。将以下行移动到 for 循环的开头:

node* newNode = new node;

否则你每次都会覆盖节点中的所有内容,然后将其重复添加到列表中,从而创建循环引用。

关于c++ - 从文本文件加载链接列表并打印时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45013955/

相关文章:

C++ 指向函数的静态成员指针——如何初始化它?

C++ 模板仿函数

java - 从 Java 打印多个 PDF 作为单个打印作业(物理打印)

java - Java 中的 LinkedList 迭代

java - 按学生 ID 对 LinkedList 进行排序

c# - 使用谓词从列表中删除元素

c++ - 为什么 `bool b = 2` 工作正常但 `bool b = {2}` 会产生缩小转换的警告?

c++ - 对抽象类的引用不能传递给线程函数?

python - 如何从 IDLE 中以彩色物理打印 python 代码?

java - 打印二维字符数组