Hash Table dictionary

Hi,
i have built a hash table dictionary based on the linked list class, the List and Meaning classes are working correctly. So i haven't included those, apologies if there is a lot of code but it is difficult to explain what i am doing without showing all the necessary code.

This code looks up a word (input by the user at a command prompt)in the hash table dictionary and outputs a message saying "word found or word not found". But what i need it also to display is the actual word together with its meaning list if there is more than one meaning. I have tried several attempts (understatement!) to use a eg.cout << word << meaninglist << endl; but i don't actually know fully how to implement the code properly.

The main function also asks the user to type in the name of an input file first-which contains all the dictionary words and their subsequent meanings

see below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cctype>
#include <cstring>
#include "IndDicT.h"
#include "List.h"
#include "Meaning.h"
#include <cassert>

using namespace std;

IndDicT Dictionary;


int main()
{
  ifstream inFile;
  string inFileName;

  cout << "Enter the name of the input file" << endl;
  cin >> inFileName;
  inFile.open(inFileName.c_str());


  IndDicT dictionary;
  string word;
  string meaning;

  assert(inFile);
  while (!inFile.eof())
    {
      string word,meaning;
      string hashKey;

       inFile >> word;
       getline(inFile, meaning);
       dictionary.insert(word, meaning);

    }
    

  string wordSearch;
  cout << "Type in a word to search: ";

  cin  >> wordSearch;
  List* found;
 
  //Testing lookUp function 
   found = dictionary.lookUp(wordSearch);
   if (found)
   {
    cout << "Word found"<<word<< endl;   //i want to include a display word and 
                                         //and meaning list to test the lookUp 
                                         // function more thoroughly?
   }
   else                                    
    {                                  
     cout << "Word not found" << endl;
    } 
  return 0;
}   

        
//This the IndDicT.cc        
     

#include <list>
#include <string>
#include <iostream>
#include <cctype>
#include <iomanip> 
#include "IndDicT.h"
#include "List.h"
#include "Meaning.h"


using namespace std;

IndDicT::IndDicT()
{
  for( int i =0;i < MAX_TABLESZ;i++)
  {
   dictionary[i].word= " ";
   dictionary[i].meaningList = new List();  
  }
}

IndDicT::~IndDicT()
{
}


int IndDicT::hashKey(string word)
{
  int result= 0;                                
  /*converts a string/char to an integer value 
   Uses the division method- divides a data item's key value
   by the total size of the hash table
   using the remainder of the division as the hash function return value
  */
  for(int i= 0; i < word.length(); i++)
    { 
      result = result + static_cast<int>(word[i]); 
    }
  
   return( result % MAX_TABLESZ );   
 } 


int IndDicT::rehashKey(string word) 
{
  int result= 0;

 for(int i= 0; i < word.length(); i++)
  {
      result = result + static_cast<int>(word[i]);
    }
 return( result % REHASH_TABLESZ ); 
}

void IndDicT::insert(string word, string meaning)
{
  int i = hashKey(word);   // calculate the table index for word
  int c = rehashKey(word); // calculates the table index for the rehash
  int home = i;
              
  cout <<""<< word <<":"<< endl;
  cout <<""<< meaning << endl << endl;               
  
 if(dictionary[i].word !=" ") // word is not an empty string
   {
   home = i; // infinite loop check
   do
   {
     i = i + c;
     if (i >= MAX_TABLESZ)
     {
      i = i - MAX_TABLESZ; 
     } 
   } while ((dictionary[i].word !=" ") && (home != i));
  } 
   if (dictionary[i].word ==" ")
     {
       dictionary[i].word= word;
       dictionary[i].meaningList->insertAtRear(meaning);
     } 
}
List* IndDicT::lookUp(string word)
{
 int i= hashKey(word);
 int c= rehashKey(word);
 int home = i;

  if (dictionary[i].word !=word)
  {
       home =i;
       c = rehashKey(word);
      do
	{
	  i = i + c;
          if(i >= MAX_TABLESZ)
	  {
	    i = i - MAX_TABLESZ;
          }
     
	}while((dictionary[i].word != word) && (home !=i));
      } 

  if (dictionary[i].word == word)
   {  
     return dictionary[i].meaningList;
   }
}


void IndDicT::traverse() 
{
   string word;
   int i= 0;
   list <string> List;


   /*Loops through the hash table/dictionary and
     Displays the word at location i of the hash table/dictionary
     Displays the list of meanings from the linked list
   */
  for (int i = 0; i < MAX_TABLESZ; i++)    
    {  
      cout << dictionary[i].word << endl;
      dictionary[i].meaningList->printList();
    }
}






i don't see your IndDicT class, so i don't really know how it works.

IndDicT::traverse() appears to print a list of meanings with dictionary[i].meaningList->printList(); so all you have to do is something like found->printList(); to print the definitions, right?

or do you mean you want to overload the << operator to print the list of definitions?

if you found the word, then your word should be in wordSearch? i don't know how to get at dictionary[i].word from the meaningList returned by InDicT::lookUp().
Topic archived. No new replies allowed.