Seperate Chaining

My code will compile however when i run linux commands on it such as

copperfield.txt is the full txt.


g++ -o hw5.exe hw5.cpp
./hw5.exe < copperfield.txt > words.txt
sort -nr < words.txt > words.sort
head -50 words.sort


it will not print to the output file.

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
  #include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;

class LinkedHashEntry {
private:
      string key;
      int value;
      LinkedHashEntry *next;
public:
      LinkedHashEntry(string key, int value) {
            this->key = key;
            this->value = value;
            this->next = NULL;
      }
 
      string getKey() {
            return key;
      }
 
      int getValue() {
            return value;
      }
 
      void setValue(int value) {
            this->value = value;
      }
 
      LinkedHashEntry *getNext() {
            return next;
      }
 
      void setNext(LinkedHashEntry *next) {
            this->next = next;
      }
};
 
int TABLE_SIZE;
 
class HashMap {
private:
      LinkedHashEntry **table;
public:
      HashMap(int size) {
            table = new LinkedHashEntry*[TABLE_SIZE];
            for (int i = 0; i < TABLE_SIZE; i++)
                  table[i] = NULL;
      }
      
      bool Search(string key, int &value)
      {
          int hash = (atoi(key.c_str()) % TABLE_SIZE);
            if (table[hash] == NULL)
                  return false;
          
        LinkedHashEntry *ptr = table[hash];
        while ((ptr != NULL) && (ptr->getKey() != key))
        ptr = ptr->getNext();

        // Return value if key found
        if (ptr != NULL)
        {
                value = ptr->getValue();
                return true;
        }
                value = 0;
        return false;
      }
      
      void Print()
      {
          int size;
          LinkedHashEntry *entry = table[0];
            TABLE_SIZE = size;
            for (int i = 0; i < TABLE_SIZE; i++)
                {
                    while(entry != NULL)
                    {
                        cout << entry->getValue() << " " << entry->getKey() << endl;
                        entry = entry->getNext();
                    }
                }    
      }
      
      int get(string key) {
            int hash = (atoi(key.c_str()) % TABLE_SIZE);
            if (table[hash] == NULL)
                  return -1;
            else {
                  LinkedHashEntry *entry = table[hash];
                  while (entry != NULL && entry->getKey() != key)
                        entry = entry->getNext();
                  if (entry == NULL)
                        return -1;
                  else
                        return entry->getValue();
            }
      }
 
      void put(string key, int value) {
            int hash = (atoi(key.c_str()) % TABLE_SIZE);
            if (table[hash] == NULL)
                  table[hash] = new LinkedHashEntry(key, value);
            else {
                  LinkedHashEntry *entry = table[hash];
                  while (entry->getNext() != NULL)
                        entry = entry->getNext();
                  if (entry->getKey() == key)
                        entry->setValue(value);
                  else
                        entry->setNext(new LinkedHashEntry(key, value));
            }
      }
 
      void remove(string key) {
            int hash = (atoi(key.c_str()) % TABLE_SIZE);
            if (table[hash] != NULL) {
                  LinkedHashEntry *prevEntry = NULL;
                  LinkedHashEntry *entry = table[hash];
                  while (entry->getNext() != NULL && entry->getKey() != key) {
                        prevEntry = entry;
                        entry = entry->getNext();
                  }
                  if (entry->getKey() == key) {
                        if (prevEntry == NULL) {
                             LinkedHashEntry *nextEntry = entry->getNext();
                             delete entry;
                             table[hash] = nextEntry;
                        } else {
                             LinkedHashEntry *next = entry->getNext();
                              delete entry;
                             prevEntry->setNext(next);
                        }
                  }
            }
      }
 
      ~HashMap() {
            for (int i = 0; i < TABLE_SIZE; i++)
                  if (table[i] != NULL) {
                        LinkedHashEntry *prevEntry = NULL;
                        LinkedHashEntry *entry = table[i];
                        while (entry != NULL) {
                             prevEntry = entry;
                             entry = entry->getNext();
                             delete prevEntry;
                        }
                  }
            delete[] table;
      }
};
//------------------------------------------------------------------
// Read a single word from cin
//------------------------------------------------------------------
bool read_word(string & word)
{
   word = "";
   bool done = false;
   while (!cin.eof() && !done)
   {
      char ch;
      cin.get(ch);
      if (ch >= 'a' && ch <= 'z')
	 word = word + ch;
      else if (ch >= 'A' && ch <= 'Z')
	 word = word + char(ch - 'A' + 'a');
      else if (word.length() > 0)
	 done = true;
   }
   return done;
}

//---------------------------------------------------------------------
// Main program to calculate word frequency
//---------------------------------------------------------------------
int main()
{
   // Create hash table for words/counts
    TABLE_SIZE = 1000;
   HashMap ht(25000);
   string word;
   while (read_word(word))
   {
      // Look up word/count in hash table
      int count = 0;
      if(!ht.Search(word, count))
      {
          ht.put(word, count + 1);
      }

      // Increment counter in hash table
      
   }
   ht.Print();
}
hey this might be better placed on the UNIX/Linux Programming forum. You can edit the post to show on that forum instead.
@Ryan: This has nothing UNIX/Linux-specific.

@mwmays: Could you use more of the standard library?

You seem to print only the LinkedHashEntries of table[0].
Topic archived. No new replies allowed.