通过在循环C++中从文件中读取文本来创建链表

Creating a Linked List via reading text from a File in a Loop C++

本文关键字:本来 链表 创建 文本来 文件 循环 C++ 读取      更新时间:2023-10-16

我有一项家庭作业,要求我从一个文本文件创建一个链表,其中有两个重要的注意事项!

  1. 无阵列

  2. 无矢量

真的,我对链表有一些不错的理解,但我似乎在程序中的某个地方失败了。通常,我会得到这些错误代码,比如无法访问遍历->下一个节点。我经常检查遍历->下一个节点,以便遍历我的链表。

现在,我没有收到任何错误代码。我的链表中有一个cout语句,它向我表明遍历节点至少(在某一点上)等于我的文本文件的输出。它在while循环中显示正确的信息,但当我打印它时……信息消失了。

我的最佳猜测是我的程序没有正确地链接到我的列表。然而,如果是这样的话,我还没能弄清楚如何正确地将它们联系起来。

有人能给我一个提示或建议吗?这实际上是赋值的第一部分(因此函数名为"InitList()"),但我很难让它发挥作用。

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

struct Node
{
    string fName;
    string lName;
    double hours;
    double pay;
    int unionCode;
    int id;
    Node *next;
}; Node *head = nullptr;
//standard node struct with a global root node so I don't have to pass it everytime

void InitList();
void AddRecord();
void PrintList();
//prototypes

int main()
{
    InitList(); 
    PrintList();
    int options;
    cout << "MENUn" <<
        "______________________________n" <<
        "1: Add a Recordn" <<
        "2: Modify a Recordn" <<
        "3: Display a Recordn" <<
        "4: Display a Summaryn" <<
        "5: Display Filen" <<
        "6: Exit Programn" << endl;
    cin >> options;

    switch (options)
    {
        case 1:
            AddRecord();
            break;
        default:
            break;
    }

}
void AddRecord()
{
    fstream file;
    file.open("PATH.txt", ios::app);
    file.close();

}
void InitList()
{
    //initializes the linked list from here on out, all changes made are    
    //reflected on the list 
    //and the text doc until the program quits
    fstream file;
    file.open("PATH.txt");
    Node *add;
    Node *traverse = head; //set traverse pointing to head

    while (file.good()) //I realize this isn't ideal, but it's temporary to get it working
    {
        add = new Node;
        file >> add->fName
            >> add->lName
            >> add->id
            >> add->pay
            >> add->hours
            >> add->unionCode;
        add->next = nullptr;
        if (traverse)
        {
            while (traverse->next)
            {
                traverse = traverse->next;
            }
            traverse->next = add;
            cout << traverse->fName << endl;
        }
        else
        {
            head = add;
            cout << head->fName << endl;
        }
    }
    file.close();
}

//simple Printing function... this is that part that's printing out ONLY the head value and nothing else (hence my assumption that head isn't linked)
void PrintList()
{
    Node *traverse = head;
    while (traverse->next)
    {
        cout << traverse->fName << " " << traverse->lName << " " << traverse->id << " $" << traverse->pay << " " << traverse->hours << " " << traverse->unionCode << endl;
        traverse = traverse->next;
    }
}
while (file.good())
{
    Node *add = new Node;
    file >> add->fName >> add->lName >> add->id >> add->pay >> add->hours >> add->unionCode;
    add->next = nullptr;
    InitList(add);
}

这就是我修复它的方法(代码位于INT MAIN())。基本上,我将数据传输到Node*add,然后将其传递给一个普通的linked_listadd函数。我通过参考。有人能解释一下为什么它以前不起作用吗?我猜这与"遍历"的值没有像我习惯的那样重置为"头"有关。否则我怎么能解决这个问题?

在循环中创建链表时,是像我那样使用迭代函数和传递遍历、迭代函数更好,还是像我失败那样使用嵌套循环的复杂系统更好?