Ich habe eine Hashtabelle von Strukturen erstellt. Jede Struktur hat eine Anzahl. Ich bin gespannt, wie ich durch jeden Schlüssel gehen und die Kette trennen und die höchste Anzahl finden und sie zu einem Array hinzufügen kann.Vergleichen von Hashtabellenwerten und Erstellen eines Arrays von Top-N-Wörtern
struct wordItem
{
std::string word;
int count;
wordItem* next;
};
Dies ist, was ich bisher habe. Mein Denkprozess besteht darin, jeden Gegenstand mit jedem Gegenstand zu vergleichen. Also gehen Sie zum Initial Key, dann durchlaufen Sie jede Kette. Vorschläge willkommen.
void HashTable::printTopN(int n) {
wordItem* arr[n];
wordItem* temp;
int i;
for (i=0;i<hashTableSize; i++){
temp = hashTable[i];
while (temp!=NULL){
for (int j = 0; j<n; j++){
if(arr[n]->count<temp->count&&arr[n+1]->count<temp->count){
arr[n]=arr[n+1];
arr[n] = temp;
}
}
temp = temp->next;
}
}
for (int k = 0; k < n; k++)
std::cout<<arr[n]->word<<"--"<<arr[n]->count;
} Auch dies ist meine addWord Funktion für mehr Hintergrundinformationen.
void HashTable::addWord(std::string word) {
int hash_val = getHash(word);
wordItem* prev = NULL;
wordItem* entry = hashTable[hash_val];
while (entry != NULL)
{
prev = entry;
entry = entry->next;
}
if (entry == NULL)
{
entry = new wordItem;
entry->count = 1;
entry->word = word;
entry ->next = NULL;
if (prev == NULL)
{
hashTable[hash_val]= entry;
}
else
{
prev->next = entry;
}
}
incrementCount(word);
entry->word = word;
}
HPP fiie
struct wordItem
{
std::string word;
int count;
wordItem* next;
};
const int STOPWORD_LIST_SIZE = 50;
class HashTable {
public:
HashTable(int hashTableSize);
~HashTable();
void getStopWords(char *ignoreWordFileName);
bool isStopWord(std::string word);
bool isInTable(std::string word);
void incrementCount(std::string word);
void addWord(std::string word);
int getTotalNumberNonStopWords();
void printTopN(int n);
int getNumUniqueWords();
int getNumCollisions();
int getHash(std::string word);
private:
wordItem* searchTable(std::string word);
int numUniqueWords;
int numCollisions;
int hashTableSize;
wordItem** hashTable;
std::vector<std::string> vecIgnoreWords =
std::vector<std::string>(STOPWORD_LIST_SIZE);
};
Wie sieht Ihre Klasse 'HashTable' aus? Außerdem hätte ich erwartet, dass in Ihrer 'for'-Schleife in 'printTopN()' ich von' 0 'statt' hashTableSize-1 'beginnen sollte. – cantordust
Das war, wenn ich von hinten die Liste lesen würde. Ich habe es aktualisiert. –
Ich verstehe. Brauchen Sie das obere "N" aus der aktuellen Kette oder aus dem gesamten Tisch? – cantordust