繁体   English   中英

在类中使用节点结构将文件读入链表C ++

[英]Reading file into linked list C++ with node structure in a class

我正在尝试将包含字母“ 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类正确地引用它们。

任何帮助将不胜感激。

一个快速解决您的问题的方法可能是:

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

由于您不再将列表节点添加到列表中,因此它会稍微改变您的逻辑,但是我仍然怀疑您不想这样做。 相反,最好将字符直接添加到列表中,并让列表处理其节点(这是有道理的,因为节点结构受保护)。

当然,该类需要更多改进,但这可以解决您的最初问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM