移除部分节点
Removing Certain Nodes
本文关键字:节点 更新时间:2023-10-16
我创建了一个数据包结构。我读取文件的文本并将每个单词插入到节点中,如果存在相同的字符串,则增加计数。但我的问题是,我只想输出相同字符串中的一个字符串和它被使用的次数。但是每当我使用remove函数时,它就会删除文件中的所有内容,如果我不使用它,就会得到如下所示的输出。我不知道我做错了什么,有一种方式,我不输出重复的字符串?
ofstream output;
struct BagNode
{
string dataValue;
string dataCopy;
int dataCountCopy;
int dataCount;
BagNode * next;
};
class Bag{
private:
BagNode * head;
public:
Bag()
{
head = NULL;
}
void insert(string v)
{
if(head == NULL){ //empty list
head = new BagNode;
removePunct(v);
head->dataValue = v;
transform(v.begin(), v.end(), v.begin(), ::tolower);
head->dataCopy = v;
head->next = NULL;
}
else
{
BagNode * n = new BagNode; // new node
removePunct(v);
n->dataValue = v;
transform(v.begin(), v.end(), v.begin(), ::tolower);
n->dataCopy = v;
BagNode * current = head; //for traversal
//current = head;
n->dataCount = 0;
if(current->dataCopy > v)
{
n->next = head;
head = n;
}
else{ //mid and tail insert
while(current->next && current->next->dataCopy < v)
{
current = current->next;
}
n->next = current->next;
current->next = n;
}
}
BagNode * check = new BagNode;
for(check = head; check->next != NULL; check = check->next)
{
if(check->dataCopy == v)//isSame(check->dataValue, v))
{
check->dataCount++;
}
}
}
bool remove(string v) //bool
{
bool status;
if(head == NULL){
status = false;
}
else if(head->dataCopy > v)
{//(head->dataValue > v){
status = false;
}
else if(head->dataCopy == v)
{
BagNode * t = head;
head = head->next;
delete t;
status = true;
}
else//general case
{
BagNode * current = head;
while(current->next && current->next->dataCopy < v){
current = current->next;
}
if(current->next == NULL)
{
status = false;
}
else if(current->next->dataCopy == v) //found it
{
BagNode *t = current->next;
current->next = current->next->next;
delete t;
status = true;
}
else
{
status = false;
}
}
return status;
}
void traverse()
{
BagNode * current;
current = head;
while(current)
{
output << current->dataValue << " (" << current->dataCount << ")" << " ";
current = current->next;
}
cout << endl;
}
输出:10Annette (1) 1805 (1) 7 (1) a (1) a (2) a (3) a (4) a (5) a (6) All (1) All (2) and (1) and (1) and (2) and (3) and (4) and (5) and (6) and (10) and (7)
if(!inputFile)
{
cout << "Could Not Open " << fileName << " File" << endl;
exit(EXIT_FAILURE);
}
else
{
while(inputFile >> text)
{
theBag.insert(text);
}
cout << "Processing File Complete" << endl;
cout << "Please Enter An Output File Name: ";
getline(cin,outputFilename);
output.open(outputFilename);
theBag.traverse();
theBag.remove(text);
inputFile.close();
output.close();
}
如果你看这里的插入函数,你实际上是触摸每个节点的值。所以如果v = "And"
每一个"And"单词的数据计数都在增加。这将使您获得每个节点上单词的正确计数。
for(check = head; check->next != NULL; check = check->next)
{
if(check->dataCopy == v)//isSame(check->dataValue, v))
{
check->dataCount++;
}
}
似乎你可以让你的插入使用的行为更简单。
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 为什么我的删除节点函数实际上没有删除节点?
- 我们可以删除链表中静态内存中的节点吗
- 如何在pugixml中获取节点的内部XML
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 引擎节点:未定义的符号:_ZTV6Config
- 为什么"delete"关键字不删除节点?
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 如何按数字顺序插入链表节点?
- 为什么我的双向链表删除函数会删除多个节点?
- 编写一个函数来删除单链表中的节点(尾部除外),仅授予对该节点的访问权限