【问题标题】:Reading file into linked list C++ with node structure in a class用类中的节点结构将文件读入链表C++
【发布时间】:2017-11-18 14:17:36
【问题描述】:

我正在尝试将包含字母“farming”的文本文件读入节点链接列表。我创建了一个名为 NumberList 的类,它具有节点的结构。这是标题。

#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;
}

这个读取文件示例来自我的教科书,但它读取的结构不在单独的类中。对于我的一生,我无法弄清楚我是如何在 NumberList 类中引用 ListNode 结构的。 Visual Studio 声明 ListNode 和 letterList 未定义。我知道这是因为我没有从 NumberList 类中正确引用它们。

任何帮助将不胜感激。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。
  • ListNode struct 似乎是来自外界的protected。这个想法可能是为了让NumberList 有一个方法(我们称之为push_back(...))将chars 添加到内部(受保护/私有)列表中。这样NumberList 对象将管理节点的创建和销毁,而在main() 中,您只需编写漂亮的list.push_back(letter),而不是每次都直接创建ListNodes。
  • 另外ListNode实际上是NumberList::ListNodeListNodeNumberList 内。 NumberList 不必完全符合条件,因为它是 NumberList

标签: c++ pointers object linked-list member-function-pointers


【解决方案1】:

您的问题的快速解决方案可能是这样的:

//------------------------------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);
}

它稍微改变了您的逻辑,因为您不再将列表节点添加到列表中, 但我怀疑你不想,无论如何。相反,最好添加字符 直接到列表,让列表处理它的节点(有意义,因为节点结构是受保护的)。

当然,该课程需要进一步完善,但这应该可以解决您最初的问题。

【讨论】:

  • 非常感谢!这确实为我指明了正确的方向。如上所述,我为我的班级创建了一个 add 方法。通过一点点工作,我就能完成任务的所有工作。
猜你喜欢
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多