我创建了一个程序,它将读取文本文件并将单词作为字符串放入链表中,以及它们在整个文本文件中的频率计数。它只打印出每个单词的一次出现次数。
我的程序还会加载一个黑名单,其中应该将黑名单链表与单词云(或单词频率)链表进行比较,然后从单词频率列表中删除列入黑名单的单词。
我尝试过这几种方式。以下是我的第3版。我想要做的是为每个节点添加一个布尔值,当一个节点等于黑名单中的一个单词时,布尔值将为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都是出于调试目的。我稍后会删除或评论这些内容。
答案 0 :(得分:0)
首先,您可以一步一步地调试,看看代码的哪一部分冻结了你的comp。检测内存泄漏的更好方法是使用Valgrind。
另外,我会将比较函数实现为比较运算符,并为其节点实现比较运算符(为方便起见)。这样做可以将代码分开一些,稍后可以帮助您了解问题所在。它也是一种更好的方法(更具可读性,OOP-y等)。
答案 1 :(得分:0)
最后!!
通过大量的旧时尚调试和cout声明,我终于得到了我想要的东西。我知道这对某些人来说可能很容易,但由于对链表没有那么熟悉,这对我来说是个过程。
在我尝试从wordList
链接列表中删除黑名单链接列表中显示的单词之前。我后来决定尝试在wordList
中的节点中添加一个布尔值true,然后调整我的print函数,使其不打印值为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:\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 *******************************************/
}
所以基本上这是一个比较函数,用于标记在另一个列表中找到的节点。运行良好,并与所有其他选项进行测试。