【问题标题】:Infinite loop when loading Linked List from Text File and printing从文本文件加载链接列表并打印时无限循环
【发布时间】:2017-12-14 07:12:58
【问题描述】:

我在从文件读取到链接列表时遇到问题。我有一种(可能是非常低效的方式)读取文件并将每个节点加载到链表中,但是如果我读取并尝试打印多于 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
...

任何人都可以看到这样做的任何原因吗?

【问题讨论】:

  • 您在示例中缺少 next node 类。我的猜测是,当您“新建”节点时,您忘记使用 NULL 初始化 newNode-&gt;nextNode

标签: c++ printing linked-list


【解决方案1】:

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

node* newNode = new node;

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

【讨论】:

  • 完美运行。谢谢一百万!
猜你喜欢
  • 1970-01-01
  • 2013-10-07
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
相关资源
最近更新 更多