【问题标题】:Cannot Get Linked List to Print from Main C++无法从主 C++ 获取要打印的链接列表
【发布时间】:2014-03-31 15:18:55
【问题描述】:

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

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

这是我的课程:

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 中,您加载到临时对象而不是当前对象。因此。摆脱newList。
  • 这绝对有效。如此简单。然而,这让我失去了尺寸。该文件有 472 个字,当我摆脱 newList 时,现在我的大小计数是 944,所以它翻了一番。你能指出为什么它现在翻倍吗?
  • 我已经写了这个作为答案,并根据您的评论进行了调整。

标签: c++ string class methods linked-list


【解决方案1】:

问题在于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。您还应该为您的析构函数重用该代码。

【讨论】:

  • 是的,我想清除它。由于某种原因,此修复使我的列表翻了一番。我将如何避免这种情况?
  • 你看到不做size++的更新了吗?问题是您是否真的有双倍的项目或尚未删除loadWordCloud 中的size++。这将使size 中的值翻倍。
  • 哦,废话,没看到我在做两次尺寸。谢谢您的帮助!!!!!我很感激。这么小的东西把我弄脏了。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2017-08-16
相关资源
最近更新 更多