C++ 比较一个链接列表与另一个黑名单与词频列表C++;

C++ 比较一个链接列表与另一个黑名单与词频列表C++;,c++,linked-list,member-functions,blacklist,word-cloud,C++,Linked List,Member Functions,Blacklist,Word Cloud,我创建了一个程序,可以读取一个文本文件,并将这些单词作为字符串放入一个链接列表,以及它们在整个文本文件中的频率计数。它只打印每个单词的一次出现及其出现的总次数 我的程序还加载了一个黑名单,它应该将黑名单链接列表与单词云(或单词频率)链接列表进行比较,然后从单词频率列表中删除黑名单中的单词 我试过几种方法。以下是我的第三个版本。我想做的是为每个节点添加一个布尔值,当一个节点等于黑名单中的一个字时,布尔值将为真。但是,我不能用下面的代码正确地打印它。我已经搜索过了,但似乎找不到向链表中的节点添加布尔

我创建了一个程序,可以读取一个文本文件,并将这些单词作为字符串放入一个链接列表,以及它们在整个文本文件中的频率计数。它只打印每个单词的一次出现及其出现的总次数

我的程序还加载了一个黑名单,它应该将黑名单链接列表与单词云(或单词频率)链接列表进行比较,然后从单词频率列表中删除黑名单中的单词

我试过几种方法。以下是我的第三个版本。我想做的是为每个节点添加一个布尔值,当一个节点等于黑名单中的一个字时,布尔值将为真。但是,我不能用下面的代码正确地打印它。我已经搜索过了,但似乎找不到向链表中的节点添加布尔值的正确语法

编辑#3:

void wordCloud::compareWith(wordCloud&wordList,wordCloud&badList){
wordNode*wordListTemp,*blacklistTemp,*temp=NULL;
无符号整数计数器=0;
对于(blacklistTemp=badList.head;blacklistTemp;blacklistTemp=blacklistTemp->next){
cout myWord(下一步){
如果(wordListTemp->myWord!=blacklistTemp->myWord){
wordListTemp->blacklist=false;
如果(wordListTemp->blacklist=false){
cout myWord myWord==blacklistemp->myWord){
cout myWord黑名单=真)

cout myWord首先,你可以一步一步地调试代码,看看哪部分代码会冻结你的代码。检测内存泄漏的更好方法是使用

另一方面,我将把这个比较函数实现为一个比较运算符,并为它们的节点实现一个比较运算符(为方便起见)。这样做会将代码分成几部分,有助于以后理解问题所在。这也是一种更好的方法(更具可读性,OOP-y,等等)。

最后

通过大量老式的调试和cout语句,我终于得到了我想要的。我知道这对一些人来说可能很容易,但由于对链表不太熟悉,这对我来说是一个相当不错的过程

在我试图从
wordList
链接列表中删除黑名单链接列表中显示的单词之前,我后来决定尝试向
wordList
中的节点添加一个布尔值true,然后将我的打印函数调整为不打印值为true的节点。我还必须在中调整
中的一些内容sertWord()
和my
freqSort()
函数,但真正包含的只是在创建新节点时添加一个指向布尔值的指针

我的成员函数是
void wordCloud::compareWith(wordCloud&wordList,wordCloud&badList)
,是我的wordCloud类的一部分。以下是定义:

void wordCloud::compareWith(const wordCloud& wordList, const wordCloud& badList){
wordNode *wordListTemp, *blacklistTemp;
unsigned int counter = 0;

//loop that advances wordListTemp
for (wordListTemp = wordList.head; wordListTemp; wordListTemp = wordListTemp->next){
    blacklistTemp = badList.head;

    //loop advances blacklistTemp - compares links in wordList to badList(blacklist)
    //and sets the node to true if myWord equals any word in the blacklist
    while (blacklistTemp){          
        if (wordListTemp->myWord == blacklistTemp->myWord){
            wordListTemp->blacklist = true; 
            counter++;
        }
        blacklistTemp = blacklistTemp->next;
    }

    //for debugging
    //cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";     
}

/*********************  All for debugging  ***************************************
cout << "True:\n\n";
wordListTemp = wordList.head;       //reset wordListTemp to head    

while (wordListTemp){               //print blacklisted words from wordList
    if (wordListTemp->blacklist == true){
        cout << wordListTemp->myWord << " <"
            << wordListTemp->freq_count << ">\n";
    }
    wordListTemp = wordListTemp->next;
}
//prints total words blacklisted
cout << "There are " << counter << " blacklisted words.";                   

cout << "\n\nFalse:\n\n";
wordListTemp = wordList.head;       //reset wordListTemp to head    
counter = 0;

while (wordListTemp){               //print non-blacklisted words from wordList
    if (wordListTemp->blacklist == false){
        cout << wordListTemp->myWord << " <"
            << wordListTemp->freq_count << ">\n";
        counter++;
    }
    wordListTemp = wordListTemp->next;
}
//prints total words not blacklisted
cout << "There are " << counter << " words that are not blacklisted.\n";

system("pause");    
********************  End debugging *******************************************/    
}
void wordCloud::compareWith(const wordCloud&wordList,const wordCloud&badList){
wordNode*wordListTemp,*blacklistTemp;
无符号整数计数器=0;
//循环,该循环用于推进wordListTemp
用于(wordListTemp=wordList.head;wordListTemp;wordListTemp=wordListTemp->next){
blacklistTemp=badList.head;
//loop advances blacklistTemp-将单词列表中的链接与坏列表(黑名单)进行比较
//如果myWord等于黑名单中的任何单词,则将节点设置为true
while(blacklistemp){
如果(wordListTemp->myWord==blacklistTemp->myWord){
wordListTemp->blacklist=true;
计数器++;
}
blacklistTemp=blacklistTemp->next;
}
//用于调试

//这是一个幼稚的实现,但为什么不做两个while循环,一个在另一个内?你在外部的黑名单上检查,在内部,你将你当前的黑名单字与你的字列表中的所有字进行比较?它显然可以被优化,但至少它会给你一个工作的开始。@o_weisman是的,也许我是l太过关注这一点。这实际上是我现在正在尝试做的。我在解决问题的代码移植方面有点慢,所以如果有任何成功,我会发回。@o_weisman-我确实尝试过,但似乎没有取得任何进展。我发布了上面的代码,但程序仍然冻结。@CharllWillia6下面的一行是错误的:如果(blacklistTemp==wordListTemp)您正在比较两个内存位置,它们显然不是相同的内存位置(如果是,则情况会更糟)。你需要将wordListTemp->myWord与blackListTemp->myWord进行比较。哦,它会冻结,因为你需要将wordListTemp=wordListTemp->next;这一行放在if块之外。我认为你无论如何都需要学习如何调试代码。如果很明显我可以在不调试的情况下看到错误,那么论坛上就不应该诚实地询问它。不是exac我知道该怎么做,或者确切地理解你在说什么。你不明白哪一部分?Valgrind还是运算符?运算符和作为比较运算符实现。我知道什么是比较运算符,但不知道如何作为比较运算符“实现”解决方案。你是在说重载吗?
void wordCloud::compareWith(const wordCloud& wordList, const wordCloud& badList){
wordNode *wordListTemp, *blacklistTemp;
unsigned int counter = 0;

//loop that advances wordListTemp
for (wordListTemp = wordList.head; wordListTemp; wordListTemp = wordListTemp->next){
    blacklistTemp = badList.head;

    //loop advances blacklistTemp - compares links in wordList to badList(blacklist)
    //and sets the node to true if myWord equals any word in the blacklist
    while (blacklistTemp){          
        if (wordListTemp->myWord == blacklistTemp->myWord){
            wordListTemp->blacklist = true; 
            counter++;
        }
        blacklistTemp = blacklistTemp->next;
    }

    //for debugging
    //cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "\n";     
}

/*********************  All for debugging  ***************************************
cout << "True:\n\n";
wordListTemp = wordList.head;       //reset wordListTemp to head    

while (wordListTemp){               //print blacklisted words from wordList
    if (wordListTemp->blacklist == true){
        cout << wordListTemp->myWord << " <"
            << wordListTemp->freq_count << ">\n";
    }
    wordListTemp = wordListTemp->next;
}
//prints total words blacklisted
cout << "There are " << counter << " blacklisted words.";                   

cout << "\n\nFalse:\n\n";
wordListTemp = wordList.head;       //reset wordListTemp to head    
counter = 0;

while (wordListTemp){               //print non-blacklisted words from wordList
    if (wordListTemp->blacklist == false){
        cout << wordListTemp->myWord << " <"
            << wordListTemp->freq_count << ">\n";
        counter++;
    }
    wordListTemp = wordListTemp->next;
}
//prints total words not blacklisted
cout << "There are " << counter << " words that are not blacklisted.\n";

system("pause");    
********************  End debugging *******************************************/    
}