Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:38:45+00:00 2026-06-02T06:38:45+00:00

This is the requirement: whenever a new word is encountered, the program should allocate

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:38:48+00:00Added an answer on June 2, 2026 at 6:38 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this requirement of Uploading 1000s of Photos from a window application. Logic
I have this weird requirement to access scanner, barcode reader, camera etc. from an
This requirement is just for simplicity for developers and beautiful code. I'm building a
I have this requirement that I need to replace URL in CSS, so far
I just read this requirement on a job listing: Aware of the pitfalls of
wondering how to carry out this requirement if at all possible. I am modifying
I realized today that I have blindly just followed this requirement for years without
Wondering if I could get some advice and direction on this following requirement: Need
This is my requirement: I have a bunch of radio box selections (types of
Requirement is this ... We have our 3 web-applications deployed in RHEL-5 server, we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.