This is the requirement: whenever a new word is encountered, the program should allocate an instance of the node from dynamic memory to contain the word and its count and insert it into a linked list so that the list is always sorted. If the word encountered already exists in the list, then the count for that word should be incremented.
I did search everywhere and the proper solution is using std::map, but I dont want to use that because I haven’t learnt that so far. Is it alright to use List or Vector and create a struct or class to manipulate each node ?
This is my proper code
class Node {
string word;
int count;
public:
Node() {
word = "";
count = 1;
}
Node(const Node &other) : word(other.word), count(other.count) {
// copy constructor
}
~Node() {} // Destructor
void printWord() const {
cout << count << " " << word << endl;
}
void loadWord(ifstream &fin) {
fin >> word;
}
void setWord(const string &word) {
this->word = word;
}
const string& getWord() const {
return word;
}
void incrementCount() {
count++;
}
};
void load(list<Node> &nodes, const char *file);
void print(const list<Node> &nodes);
bool isExist(const list<Node> &nodes, const string &word, Node &node);
void error(const string &message, const char *file);
const Node& getNode(const list<Node> &nodes, const string &word);
int main(int argc, char *argv[]) {
list<Node> nodes;
if (argc != 2) {
cout << "Error syntax : require an input file\n";
return 0;
}
load(nodes, argv[1]);
print(nodes);
return 0;
}
void print(const list<Node> &nodes) {
list<Node>::const_iterator itr;
for (itr = nodes.begin(); itr != nodes.end(); itr++) {
itr->printWord();
}
cout << '\n';
}
void load(list<Node> &nodes, const char *file) {
ifstream fin;
Node node;
string temp;
fin.open(file);
if (!fin)
error("Cannot open file ", file); // exit
while (!fin.eof()) {
if (fin.good()) {
fin >> temp;
if (!isExist(nodes, temp, node)) {
node.setWord(temp);
nodes.push_back(node);
} else {
// increase word count here
}
} else if (!fin.eof())
error("Unable to read data from ", file);
}
fin.close();
}
bool isExist(const list<Node> &nodes, const string &word, Node &node) {
list<Node>::const_iterator itr;
for (itr = nodes.begin(); itr != nodes.end(); itr++) {
if(word.compare(itr->getWord()) == 0) {
return true;
}
}
return false;
}
const Node& getNode(const list<Node> &nodes, const string &word) {
list<Node>::const_iterator itr;
for (itr = nodes.begin(); itr != nodes.end(); itr++) {
if(word.compare(itr->getWord()) == 0) {
return *itr;
}
}
return NULL; // This is fail what should I do to return a NULL value when not found
}
void error(const string &message, const char *file) {
cerr << message << file << '\n';
exit(0);
}
The code doesn’t work, I just tried to generate my solution to solve the question by applying my Java knowledge but it seems diffirent to control object in c++. Could someone examine my code and suggest me a better way ?
Thanks.
No, this is not the best task to practice vectors and lists. You really have to look at std::map documentation and write 3 lines of efficient, nice looking code.
Why didn’t you apply your TreeMap Java knowledge?