将文件读入具有 class 节点结构的链表 C++
Reading file into linked list C++ with node structure in a class
我正在尝试将包含字母 "farming" 的文本文件读取到节点链表中。我创建了一个名为 class 的 NumberList,它具有节点的结构。这是 header.
#ifndef NUMBERLIST
#define NUMBERLIST
#include <iostream>
using namespace std;
class NumberList
{
protected:
//declare a class for the list node
//constructor to initialize nodes of list
struct ListNode
{
char value;
ListNode *next;
// Constructor
ListNode(char value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode *head; //pointer to head of the list
public:
NumberList() { head = NULL; } //constructor
~NumberList(); //destructor
void displayList() const; //print out list
void reverse();
};
#endif
我 运行 遇到的问题是尝试将文本文件读入 main() 中的链表。
这是我的主要内容:
#include "Numberlist.h"
#include "ReliableNumberList.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ListNode *letterList = nullptr; //create a linked list
char letter;
//This is where I read the file into the list
//open the file
ifstream letterFile("linkedText.txt");
if (!letterFile)
{
cout << "Error in opening the file of letters.";
exit(1);
}
//read the file into a linked list
while (letterFile >> letter)
{
//create a node to hold this letter
letterList = new ListNode(letter, letterList);
//missing a move to the next node?
}
return 0;
}
此读取文件示例来自我的教科书,但其读取的结构不在单独的 class 中。对于我的一生,我无法弄清楚如何在 NumberList class 中引用 ListNode 结构。 Visual Studio 表示 ListNode 和 letterList 未定义。我知道是因为我没有从 NumberList class 中正确引用它们。
如有任何帮助,我们将不胜感激。
您的问题的快速解决方案可能是:
//------------------------------NumberList.hpp-----------------------------
#ifndef NUMBERLIST_HPP
#define NUMBERLIST_HPP
#include <iostream>
class NumberList{
protected:
//Protected Members can't be used outside the class
struct ListNode{
char value;
ListNode *next;
ListNode(char value1, ListNode *next1 = NULL){
value = value1;
next = next1;
}
};
ListNode *head, *tail; //class members
//head always points at the 1st letter, tail is used for quick adding at the end
public:
NumberList() { head = NULL; tail = NULL; }
~NumberList(); //don't forget to deallocate space properly at the end
void displayList() const; //print out list
void reverse();
void add(char newchar) {
//allocate a new node using the ListNode constructor, by default, next1 will be null
ListNode *newNode = new ListNode(newchar); //equvalent to ListNode(newchar, NULL);
if (tail == NULL) { //if no elements in the list, both show to newNode
tail = newNode;
head = newNode;
}else{
tail->next = newNode; //make last node -> next pointer, point to newNode (new last node)
tail = tail->next; //make current last node be the actual last node
}
}
};
#endif
//------------------------------Main.cpp-----------------------------
#include "Numberlist.hpp"
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream letterFile("linkedText.txt");
if (!letterFile){
cout << "Error in opening the file of letters.";
exit(-1);
}
NumberList numberList;
char letter;
while (letterFile >> letter) numberList.add(letter);
}
它稍微改变了您的逻辑,因为您不再向列表中添加列表节点,
但我怀疑你不想,无论如何。相反,最好添加字符
直接到列表,并让列表处理它的节点(有意义,因为节点结构是受保护的)。
当然 class 需要更多改进,但这应该可以解决您最初的问题。
我正在尝试将包含字母 "farming" 的文本文件读取到节点链表中。我创建了一个名为 class 的 NumberList,它具有节点的结构。这是 header.
#ifndef NUMBERLIST
#define NUMBERLIST
#include <iostream>
using namespace std;
class NumberList
{
protected:
//declare a class for the list node
//constructor to initialize nodes of list
struct ListNode
{
char value;
ListNode *next;
// Constructor
ListNode(char value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode *head; //pointer to head of the list
public:
NumberList() { head = NULL; } //constructor
~NumberList(); //destructor
void displayList() const; //print out list
void reverse();
};
#endif
我 运行 遇到的问题是尝试将文本文件读入 main() 中的链表。
这是我的主要内容:
#include "Numberlist.h"
#include "ReliableNumberList.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ListNode *letterList = nullptr; //create a linked list
char letter;
//This is where I read the file into the list
//open the file
ifstream letterFile("linkedText.txt");
if (!letterFile)
{
cout << "Error in opening the file of letters.";
exit(1);
}
//read the file into a linked list
while (letterFile >> letter)
{
//create a node to hold this letter
letterList = new ListNode(letter, letterList);
//missing a move to the next node?
}
return 0;
}
此读取文件示例来自我的教科书,但其读取的结构不在单独的 class 中。对于我的一生,我无法弄清楚如何在 NumberList class 中引用 ListNode 结构。 Visual Studio 表示 ListNode 和 letterList 未定义。我知道是因为我没有从 NumberList class 中正确引用它们。
如有任何帮助,我们将不胜感激。
您的问题的快速解决方案可能是:
//------------------------------NumberList.hpp-----------------------------
#ifndef NUMBERLIST_HPP
#define NUMBERLIST_HPP
#include <iostream>
class NumberList{
protected:
//Protected Members can't be used outside the class
struct ListNode{
char value;
ListNode *next;
ListNode(char value1, ListNode *next1 = NULL){
value = value1;
next = next1;
}
};
ListNode *head, *tail; //class members
//head always points at the 1st letter, tail is used for quick adding at the end
public:
NumberList() { head = NULL; tail = NULL; }
~NumberList(); //don't forget to deallocate space properly at the end
void displayList() const; //print out list
void reverse();
void add(char newchar) {
//allocate a new node using the ListNode constructor, by default, next1 will be null
ListNode *newNode = new ListNode(newchar); //equvalent to ListNode(newchar, NULL);
if (tail == NULL) { //if no elements in the list, both show to newNode
tail = newNode;
head = newNode;
}else{
tail->next = newNode; //make last node -> next pointer, point to newNode (new last node)
tail = tail->next; //make current last node be the actual last node
}
}
};
#endif
//------------------------------Main.cpp-----------------------------
#include "Numberlist.hpp"
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream letterFile("linkedText.txt");
if (!letterFile){
cout << "Error in opening the file of letters.";
exit(-1);
}
NumberList numberList;
char letter;
while (letterFile >> letter) numberList.add(letter);
}
它稍微改变了您的逻辑,因为您不再向列表中添加列表节点, 但我怀疑你不想,无论如何。相反,最好添加字符 直接到列表,并让列表处理它的节点(有意义,因为节点结构是受保护的)。
当然 class 需要更多改进,但这应该可以解决您最初的问题。