c++ - 无法从主 C++ 获取要打印的链表

标签 c++ string class methods linked-list

我正在开发一个词云 C++ 程序,该程序假设从文本文件中填充一个词列表,将这些词放入一个链表中,然后按频率对列表进行排序,并仅使用一个实例打印列表单词和单词旁边的频率。

我完成的是读取文件、填充链表并打印它。但是,我无法从 main 而不是 wordCloud 方法 loadWordCloud() 获取要打印的列表。我还不擅长上课,所以我确定这是我的问题。但基本上我想从 main 中调用 printWordCloud() 方法,而不是从 loadWordCloud() 中打印。当我在调用 getList.loadWordCloud(myFile) 之后放置 getList.printWordCloud() 时,我得到一个空列表。我明白为什么会这样,但我不知道如何从 loadWordCloud() 中实际保存列表,以便稍后打印。我试图避免返回语句并使方法无效。

这是我的类(class):

class wordNode{
public:
    string myWord;
    int freq_count;
    bool black_list;
    wordNode *next;

    wordNode(string aWord);
    ~wordNode(void){};
};

wordNode::wordNode(string aWord){
     myWord = aWord;
     next = NULL;
     freq_count = 0;
}

class wordCloud{
public:
    wordNode *head;
    int size;
    wordNode *nextWord;

    wordCloud(void);
    ~wordCloud(void){};

    void insertWord(string aWord) { insertWord(aWord, false); }
    void insertWord(string aWord, bool blacklist);
    /*void insertWordDistinct(string aWord);

    void loadBlacklist(string fileName);*/
    void loadWordCloud(string fileName);

    //void printBlacklist(void);        // Print the words in the blackList

    void printWordCloud() { printWordCloud(1); }
    void printWordCloud(int freq);      // Print the words with freq or greater freq_count 

    /*void Initialize(void) { nextWord = head; }
    void GetNextWord(string theWord, int *freq_count);

    void freqSort(string sortOrder);    // 'A' = ASC, 'D' = DESC 

private:
    void properlyHandleHeadofList(string sortOrder);
    void properlyHandleRestofList(string sortOrder);
    string upperCase(string text);
    string lowerCase(string text);*/
};

/*void wordCloud::GetNextWord(string theWord, int *freq_count){
    wordCloud aWord;

    aWord.insertWord(theWord);
}*/

wordCloud::wordCloud(void){
    head = NULL;
    size = 0;
    nextWord = NULL;
}

void wordCloud::insertWord(string aWord, bool blacklist){
    wordNode *newWord = new wordNode(aWord);

    if (head == NULL)
        head = newWord;
    else{
        newWord->next = head;
        head = newWord;
    }
    size++;
}

void wordCloud::printWordCloud(int freq){
    wordNode *temp;

    if (head == NULL)
        cout << "No Word Cloud" << endl;
    else{
        temp = head;

            while (temp != NULL){
                cout << temp->myWord << endl;
                temp = temp->next;
            }
    }
    system("pause");
}

void wordCloud::loadWordCloud(string fileName){
    ifstream file;          //variable for fileName
    string word;
    wordCloud newList;

    //newList.Initialize();

    file.open(fileName);    //open file

    while (!file.eof()){
        file >> word;       //grab a word from the file one at a time
        transform(word.begin(), word.end(), word.begin(), 
            ::tolower);     //automatically change words to lowercase
        newList.insertWord(word);
        size++;             //increment size
        //cout << word <<'\n';  //print word - for debugging
    }

    newList.printWordCloud();   //print word cloud - debugging - works
    cout << size << '\n';       //print size - for debugging - works

    file.close();
    system("pause");
}

这是我的主要内容:

int main(){
    string myFile = "words.txt";
    wordCloud getList;

    //For debugging
    //list.insertWord("word");
    //list.insertWord("more");

    getList.loadWordCloud(myFile);
}

这个计划远未完成。我还必须找出词频是如何做的,并在一个词出现两次时删除一个节点,但现在,我只想知道如何从 main 打印。任何帮助将不胜感激。

最佳答案

问题在于,在 loadWordCloud 中,您有一个要加载到的局部变量。相反,您应该加载当前对象。这样,getList 变量就会有正确的内容。

void wordCloud::loadWordCloud(string fileName)
{
    ifstream file;          //variable for fileName
    string word;

    file.open(fileName);    //open file

    while (!file.eof()){
        file >> word;       //grab a word from the file one at a time
        transform(word.begin(), word.end(), word.begin(), 
            ::tolower);     //automatically change words to lowercase
        insertWord(word);
        //Don't increment size here, insertWord does this! size++;
        //cout << word <<'\n';  //print word - for debugging
    }
    printWordCloud();   //print word cloud - debugging - works
    cout << size << '\n';       //print size - for debugging - works

    file.close();
    system("pause");
}

您还需要删除 size++,因为 insertWord 会为您执行此操作(否则您会得到 size 双倍的字数) .

如果你想多次调用loadWordCloud并且想清除列表而不是添加,你应该添加一个函数来清除列表并在所有的调用delete节点。您还应该为您的析构函数重新使用该代码。

关于c++ - 无法从主 C++ 获取要打印的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22081319/

相关文章:

c# - 如何接受 "$1,250.00"之类的字符串并将其转换为 C# 中的小数?

c++ - 将类中的所有 double 值初始化为零

c++ - 在 C++ 中出现 "scope"错误

c - 从函数返回指针数组

ruby - 为什么 Ruby 返回 `str[-1..1]` 它做了什么?

swift - 我应该对存储在 Swift 数组中的元素使用类还是结构

python - 如何在 Python 中查找绑定(bind)方法的实例?

c++ - 为什么我会收到创建基础类(class)的警告?

c# - 如何在.net框架上使用vst sdk

java - Class.forName() 与直接类加载