greatman05 0 Newbie Poster

This is homework.

I am trying to implement a Dictionary ADT using a hash-table with a closed addressing scheme. The problem is that I'm required to make a hash table of Word classes. How can I do this? The class has to be templated.

Here is what I have so far:

#include <string>
#include <fstream>
using namespace std;

#ifndef DICTIONARY_H
#define DICTIONARY_H

template <typename DataType>
class Dictionary
{
    private:
        int size_;
        int words;
        int Hash(DataType);
        DataType* dictionary;
        class Word
    {
        private:
            Dictionary<string> dict;
            int key;
            string word_;
        public:
            Word(string word_)
            {
                this->word_ = word_;
                Key(word_);
            }
            void Key(string key_value)
            {
                int total = 0;
                for (int i = 0; i < key_value.size(); i++)
                {
                    total += key_value[i];
                }
                int key_ = total % dict.size_;
                while(dict.dictionary[key_] != "")
                {
                    int key2 = key*3 + 5;
                    key_ = (key_ + key2) % dict.size_;
                }
                key = key_;
            }
            int getKey()
            {
                return key;
            }
            string getWord()
            {
                return word_;
            }
};  
    public:
        void add(Word&);
        void find(DataType&);
        bool contains(DataType&);
        int size();
        Dictionary(int, string);
        Dictionary();
        ~Dictionary();
        Dictionary(const Dictionary&);
        Dictionary operator=(const Dictionary&);
};


template <typename DataType>
Dictionary<DataType>::~Dictionary()
{
    delete [] dictionary;
}

template <typename DataType>
Dictionary<DataType>::Dictionary()
{
    dictionary = NULL;
    size_ = words = 0;
}

template <typename DataType>
Dictionary<DataType>::Dictionary(int in_size, string filename)
{
    ifstream inFile(filename.c_str());
    size_ = in_size+1;
    dictionary = new DataType [size_];
    string word;
    while (!inFile.eof())
    {
        getline(inFile, word);
        Word new_word(word);
        add(new_word);
    }
}


template <typename DataType>
Dictionary<DataType>::Dictionary (const Dictionary& rhs)
{
    size_ = rhs.size_;
    words = rhs.words;
    dictionary = new DataType[size_];
    for (int i = 0; i < size; i++)
        dictionary[i] = rhs.dictionary[i];
}

template <typename DataType>
Dictionary<DataType> Dictionary<DataType>::operator=(const Dictionary& rhs)
{
    if (this != &rhs)
    {
        if (size_ > 0)
            delete [] dictionary;
        size_ = rhs.size_;
        words = rhs.words;
        dictionary = new DataType[size_];
        for (int i = 0; i < size_; i++)
            dictionary[i] = rhs.dictionary[i];
    }
    return *this;
}

template <typename DataType>
void Dictionary<DataType>::add(Word& in_word)
{
    dictionary[in_word.getKey()] = in_word;
    words++;
}

template <typename DataType>
void Dictionary<DataType>::find(DataType& word)
{
    try
    {
        if (dictionary[word.getKey()] = word)
            dictionary[word.getKey()] = NULL;
        else
        {
            throw ("Object not found.");
        }
    }
    catch (error &e)
    {
        cout<<&e;
    }
}

template <typename DataType>
bool Dictionary<DataType>::contains(DataType& word)
{
    int key = hash(word);
    if (dictionary[key] = word)
        return true;
    return false;
}

template <typename DataType>
int Dictionary<DataType>::size()
{
    return size_;
}


#endif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.