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



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

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

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

编辑#3:

void wordCloud::compareWith(wordCloud& wordList, wordCloud& badList){
wordNode *wordListTemp, *blacklistTemp, *temp = NULL;
unsigned int counter = 0;
for (blacklistTemp = badList.head; blacklistTemp; blacklistTemp = blacklistTemp->next){
cout << blacklistTemp->myWord << "n";
for (wordListTemp = wordList.head; wordListTemp; wordListTemp = wordListTemp->next){
if (wordListTemp->myWord != blacklistTemp->myWord){
wordListTemp->blacklist = false;
if (wordListTemp->blacklist = false){
cout << wordListTemp->myWord << " <"
<< wordListTemp->freq_count << ">n";
}
}
else if (wordListTemp->myWord == blacklistTemp->myWord){
cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "n";
wordListTemp->blacklist = true;
if (wordListTemp->blacklist = true)
cout << wordListTemp->myWord << "n";
} 
}
//counter++;
cout << blacklistTemp->myWord << " " << wordListTemp->myWord << "n";
}
system("pause");
}

这还不完全,但这是我所能做到的。问题是它只打印true if,不会打印任何false if。即使我切换值,它仍然只打印true if。所以我认为我做这件事是错的。"标记"一个节点为true和"标记"节点为false的正确方法是什么?所有的cout都是为了调试目的。我稍后会删除或评论这些内容。

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

顺便说一句,我将把比较函数实现为比较运算符,并为它们的节点实现比较运算符(为了方便)。这样做可以稍微划分代码,有助于以后了解问题所在。这也是一种更好的方法(更可读、OOP-y等)。

终于!!

经过大量的旧时尚调试和高级定制声明,我终于得到了我想要的。我知道这对一些人来说可能很容易,但由于不太熟悉链表,这对我来说是一个很好的过程

之前我试图从wordList链接列表中删除黑名单链接列表中的单词。后来我决定只尝试将布尔值true添加到wordList中的节点,然后调整我的打印函数,使其不打印值为true的节点。我还必须调整insertWord()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:nn";
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 << "nnFalse:nn";
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 *******************************************/    
}

所以基本上这是一个比较函数,它标记在另一个列表中找到的节点。工作良好,与所有其他选项一起测试。

最新更新