无法获取从主C ++打印的链接列表

时间:2014-02-27 21:54:18

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

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

我所做的就是阅读文件,填充链表并打印它。但是我无法从main而不是wordCloud方法loadWordCloud()获取列表。我还没上课,所以我确信这是我的问题。但基本上我想从main调用printWordCloud()方法,而不是从loadWordCloud()打印。当我在调用getList.printWordCloud()之后添加getList.loadWordCloud(myFile)时,我会得到一个空列表。我可以看到为什么会发生这种情况,但我无法弄清楚如何在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打印的。非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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。您还应该为析构函数重用该代码。