通过在循环C++中从文件中读取文本来创建链表
Creating a Linked List via reading text from a File in a Loop C++
我有一项家庭作业,要求我从一个文本文件创建一个链表,其中有两个重要的注意事项!
-
无阵列
-
无矢量
真的,我对链表有一些不错的理解,但我似乎在程序中的某个地方失败了。通常,我会得到这些错误代码,比如无法访问遍历->下一个节点。我经常检查遍历->下一个节点,以便遍历我的链表。
现在,我没有收到任何错误代码。我的链表中有一个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函数。我通过参考。有人能解释一下为什么它以前不起作用吗?我猜这与"遍历"的值没有像我习惯的那样重置为"头"有关。否则我怎么能解决这个问题?
在循环中创建链表时,是像我那样使用迭代函数和传递遍历、迭代函数更好,还是像我失败那样使用嵌套循环的复杂系统更好?
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 打印所有链表的元素 (C++)
- 错误:需要类名(链表c++)
- 为什么C中的通用链表中存储的数据已损坏
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表c++插入,所有情况都已检查,但没有任何工作
- 链表,将列表复制到另一个列表
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 对单向链表进行排序时出现运行时错误