比较一个链接列表与另一个黑名单与词频列表C++
Comparing One Link List to Another Blacklist versus Word Frequency List 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 *******************************************/
}
所以基本上这是一个比较函数,它标记在另一个列表中找到的节点。工作良好,与所有其他选项一起测试。
- 链表,将列表复制到另一个列表
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- 如何将列表从一个类复制到另一个类
- 将列表的对象C++移动到另一个对象
- 通过另一个宏创建的函数创建所有列表的宏
- 在由邻接列表表示的树中查找节点到另一个给定节点之间的路径
- 如何制作一个结构程序,在其中可以存储无限量的数据,以便每次您想要时都可以将另一个产品添加到列表中?
- 让构造函数在其初始化列表中调用同一类的另一个构造函数是否有效
- 一种体面的方式来转换const列表参考参数,然后传递到另一个函数
- 将元素从一个向量复制到另一个向量的最有效方法,给定一个不被复制的索引列表
- 维护多集中数字的排序列表和另一个集合中的累积总和
- 将带有指针的 STD 列表复制到另一个带有指针的 STD 列表
- 比较一个链接列表与另一个黑名单与词频列表C++
- 从文本文件将整数读入简单的链表中.然后对整数列表进行气泡排序并读出到另一个文件
- 在 std 列表中查找对象并将其添加到另一个列表
- 将类的实例传递给另一个构造函数,该构造函数将其对象添加到所传递实例所拥有的列表中
- 在c++中遍历一个列表并在另一个列表中搜索每个元素
- 如何将参数列表传递给c++中的另一个函数
- 如何通过赋值运算符重载将一个列表复制到另一个列表上?C++
- 通过初始化列表调用另一个类的构造函数.有问题