我正在使用链接列表来实现一致性程序。如果多次读取相同的单词,我需要删除当前节点,增加计数,并添加新节点。我无法再为此程序添加任何功能。我想我不得不以某种方式使用get_count,但我不确定。
例如,不是看起来像这样:
THE 1
1
应该是:
THE 2
我怎样才能做到这一点?提前谢谢!
标题文件:
#ifndef CONCORDANCE_H
#define CONCORDANCE_H
#include <iostream>
#include <cstdlib>
const int MAX = 8;
class Concordance
{
public:
typedef char Word[MAX+1];
// CONSTRUCTOR
Concordance()
{
first = NULL;
}
// DESTRUCTOR
~Concordance();
// MODIFICATION MEMBER FUNCTIONS
void insert(Word& word, int& n);
void remove(Word& word);
int get_count(Word& word);
// OTHER FUNCTIONS
int length() const;
friend std::ostream& operator << (std::ostream& out_s, Concordance& c);
private:
// NODE STRUCT
struct Node
{
Word wd;
int count;
Node *next;
};
Node *first;
// GET_NODE FUNCTION
Node* get_node(Word& word, int& count, Node* link);
};
#endif
类别:
//class definition
#include "concordance.h"
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
Concordance::~Concordance()
{
Node *temp;
while(first != NULL)
{
temp = first;
first = first -> next;
delete temp;
}
}
void Concordance::insert(Word& word, int& n)
{
Node *prev;
if(first == NULL || strcmp(first -> wd, word) > 0)
first = get_node(word, n, first);
else
{
prev = first;
while(prev -> next != NULL && strcmp(prev -> next -> wd, word) < 0)
prev = prev -> next;
prev -> next = get_node(word, n, prev -> next);
}
}
void Concordance::remove(Word& word)
{
Node *prev, *temp;
prev = temp;
if(prev -> wd == word)
{
first = first -> next;
delete prev;
}
else
{
while(strcmp(prev -> next -> wd, word) > 0)
prev = prev -> next;
temp = prev -> next;
prev -> next = temp -> next;
delete temp;
}
}
int Concordance::get_count(Word& word)
{
while(strcmp(first -> wd, word) != 0)
first = first -> next;
return first -> count;
}
int Concordance::length() const
{
Node *cursor;
int length;
length = 0;
for(cursor = first; cursor != NULL; cursor = cursor -> next )
length++;
return length;
}
Concordance::Node* Concordance::get_node (Word& word, int& count, Node* link)
{
Node *temp;
temp = new Node;
strcpy(temp-> wd, word);
temp-> next = link;
temp -> count = count+1;
return temp;
}
ostream& operator << (ostream& out_s, Concordance& c)
{
Concordance::Node *output;
out_s << "Word" << setw(10) << " " << "Count" << setw(8) << endl;
out_s << "--------------------" << endl;
for(output = c.first; output != NULL && output->next != NULL; output = output-> next )
out_s << left << setw(10) << output-> wd << right << setw(9) << output -> count << endl;
if(output != NULL)
out_s << output-> wd << setw(13) << " " << output -> count << endl;
out_s << "--------------------" << endl;
return out_s;
}
答案 0 :(得分:0)
基本上,您需要在列表中搜索现有单词,如果找到,只需在现有节点结构中递增计数:
Node* existing = first;
while (strcmp(existing->wd, word) != 0)
existing = existing->next;
if (existing)
++existing->count;
else
{
// not already existing, so add new node
}
但如果你被允许,我强烈建议改用std::map
- 因为几乎所有的东西都是为你处理的:
std::map<std::string, int> mapWords;
// for each word:
++mapWords[word];
这两行代码几乎可以替换所有链接列表代码,另外一个好处是查找速度会快得多,因为地图使用二叉树而不是必须线性搜索的链表。
如果你有一个C ++ 11编译器,你也可以使用unordered_map
使用哈希表,因为地图中的元素没有保持排序,所以可以更快。