将文件读入带有类中节点结构的链表C ++

时间:2017-06-15 23:24:33

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

我正在尝试将包含字母“耕种”的文本文件读入节点的链接列表中。我创建了一个名为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类中正确引用它们。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

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

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

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

当然,课程需要更多精炼,但这应该可以解决你最初的问题。