vendredi 26 février 2021

How to calculate word frequency in a list using C++?

So I have some words in a file. I read them to a List then I'm trying to find the frequency of each word. My problem is that I have to follow a certain implementation for the list which isn't very flexible. Here's the List class:

const int maxListSize = 50;

template<class T>
class List {
private:
    int numberOfElements;
    int currentPosition;
    T data[maxListSize];
public:
    List() {
        numberOfElements = 0;
        currentPosition = -1;
    }
    void insert(T element) {
        if (numberOfElements >= maxListSize) {
            cout << "List is Full" << endl;
            return;
        }
        data[numberOfElements] = element;
        numberOfElements++;
    }

    bool first(T &element) {
        if (numberOfElements == 0) {
            cout << "List is Empty" << endl;
            return false;
        }
        else {
            currentPosition = 0;
            element = data[currentPosition];
            return true;
        }
    }

    bool next(T &element) {
        //Check if the user called the first function
        if (currentPosition < 0) {
            cout << "Please call the first function before calling the next" << endl;
            return false;
        }
        if (currentPosition >= numberOfElements - 1) {
            //cout << "No next item" << endl;
            return false;
        }
        currentPosition++;
        element = data[currentPosition];
        return true;
    }
};

Assume my list is called names. How can I get the frequency of each word?

Aucun commentaire:

Enregistrer un commentaire