Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of linked lists in C++

Some code for context:

class WordTable
{
    public:
        WordTable();
        ~WordTable();

        List* GetListByAlphaKey(char key);
        void AddListByKey(char key);
        bool ListExists(char key);
        bool WordExists(string word);
        void AddWord(string word);
        void IncrementWordOccurances(string word);
        void Print();
    private:     
        List *_listArray[33];
        int _GetIndexByKey(char key);
};


class TableBuilder
{
    public:
    TableBuilder();
    ~TableBuilder();
    void AnalyzeStream(fstream &inputStream);        
    void PrintResults();
    private:
        void _AnalyzeCursor(string data);
        bool _WordIsValid(string data);
        WordTable* _WordTable;        
};

struct Element {
public:
   string Word;
   int Occurances;
   Element* Next;
};


class List
{
    public:
        List();
        ~List();

        Element* AddElement(string word);       
        void DeleteElement(Element* element);       
        void Print();       
        void Delete();
        Element* First;
        bool WordExists(string word);
        void IncrementWordOccurances(string word);      
    private:
        void _PrintElementDetails(Element* element);
};

Requirements
I must analyze text, building array of linked lists (where array contains list for each letter; list contains every word found in text), then print out results.

Problem I can`t initialize array of lists in WordTable.cpp. I know that i've misunderstood something, but i got no ideas and time. Anyone?

P.s. Yeah, that's a homework. STOP giving me advices about best practices, please... :)

like image 442
Arnis Lapsa Avatar asked Dec 07 '25 12:12

Arnis Lapsa


1 Answers

An initialization for _listArray would look like this:

WordTable::WordTable() {
  for (int i=0; i<33; i++)
    _listArray[i] = new List();
}

You don't really say what exactly the problem is so I'm not sure if this helps...

like image 152
sth Avatar answered Dec 10 '25 01:12

sth