2

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... :)

7
  • Looks like you need a space in between your star and your variable name: List *_listArray[33]; Is this a problem in the actual code? Commented May 10, 2009 at 22:51
  • 6
    No, you don't need a space. It doesn't affect the code's correctness at all. Commented May 10, 2009 at 22:53
  • This is homework according to a comment below. You need to label it as such. Commented May 11, 2009 at 12:59
  • I cannot see the reason you shouldn't follow good practices when doing homeworks... Commented May 12, 2009 at 10:28
  • 2
    Because we are not supposed to do your homework for you. Your homework is so you can learn. If we do it for you, you won't learn. That is how it influences answers. Commented May 12, 2009 at 13:03

4 Answers 4

4

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...

Sign up to request clarification or add additional context in comments.

8 Comments

Note: Use of magic numbers (33: what does it stand for??) is usually a bad idea.
Not always hard coding something is bad. 33 are letters of alphabet. I'm quite sure they won`t change next few days until my homework will be delivered. :)
sth, sorry, this one didn`t help. I tried it already. Throws segmentation error.
It is considered good practice to declare any constant numbers using const int letters = 33; or #define letters 33 just to improve readability and maintainability
This answer is correct with the code you've given. Perhaps you need to check what List's constructor does, if this code crashes.
|
3

It's because you're creating an array of pointers to lists. Either change it to List _listArray[33]; or initialize it like so:

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

// and to access methods
_listArray[i]->AddElement( "word" );

2 Comments

This is the problem, initialization in this way throws an error right after it tries to use indexer on _listArray.
Could you post the definition of the List class, instead of just the declaration?
3

It appears you are optimizing your linked lists by having an array of them, one for each first letter of the word. Don't do that.

Use std::map. Where the string is the word and the int is your count.

EDIT: If you ignore my advice... As has been pointed out your _listArray is actually an array of pointers, not an array of objects. I think you wanted an array of objects. Since the array is fixed length, and List has a default constructor, the simplest way to do that is to just say

List _listArray[33];

If you want dynamic allocation, you could do this instead:

List* _listArray;

And in the constructor:

_listArray = new List[33];

And in the destructor:

delete[] _listArray;

6 Comments

While true, this does not adresses the poster's question re: how to initialize a linked list table.
Arguably, one should not be making linked lists in C++. The STL implementation of List will work as well as or better than a hand spun linked list. Additionally the use for the linked list here is dubious as the poster intends to search it often and a linked list is not a proper data structure for frequent search. Simply answering the user's question is of less value than helping the user find a solution.
"It appears you are optimizing your linked lists by having an array of them, one for each first letter of the word. Don't do that." - This is exactly what i'm trying to do. Sadly that's one of the rules of my homework - to use self-made array of linked lists.
jmucchiello, i appreciate your good will, but i just need to understand it and get it done. no need for optimization tips&tricks.
You should have said it was homework and you should have said you HAD to use an array of linked lists
|
0

Please don't reinvent the wheel (again) Use std::list it works correctly, is safer, is standard.

Second : you have design problems : element attribute in list is public, but you have a private method ???.

3 Comments

Reinventing the wheel is what he needs - that`s a homework. dooh.
And if is a homework, please when you describe the problem , describe exactly the requirements : for example : extract words from a text without using any c++ library.
Sorry for wasting your time... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.