2017-05-16 1 views
0

J'essaie de créer un projet de table de hachage simple. Je testais mon programme quand j'ai remarqué que la fonction addPlayer ne fonctionnait pas. addPlayer repose sur quelques autres fonctions mais j'ai également vérifié ces fonctions et je ne trouve pas l'erreur. Toute aide à ce sujet serait vraiment appréciée, si je pouvais diagnostiquer cela moi-même. Le programme dans son intégralité est ci-dessous:Table de hachage addFunction

tête de fichier (Player.h)

#include <iostream> 
#include <string> 

    using namespace std; 

    #ifndef PLAYER_H 
    #define PLAYER_H 

    class Player 
    { 
    private: 
     static const int tableSize = 10; 

     struct player 
     { 
      string name; 
      string race; 
      player* next; 
     }; 

     player* HashTable[tableSize]; 

    public: 
     //Constructor 
     Player(); 
     //Hash function 
     int hash(string key); 
     //Adds players and takes in their attributes 
     void addPlayer(string name, string race); 
     //Counts number of players in index 
     int numPlayersInIndex(int index); 
     //Prints information from the items held in the hash table 
     void printTable(); 
    }; 
    #endif 

Main (Main.cpp)

#include <cstdlib> 
#include <iostream> 
#include <string> 

//Custom headers 
#include "Player.h" 
//Pause console before exiting window 
#include <stdlib.h> 

#ifdef _WIN32 
#define WINPAUSE system("pause") 
#endif 

using namespace std; 

int main(int argc, char** argv) 
{ 
    //Test addPlayer 
    Player player1; 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("hapdsfdsfsdsdfsdfsdfpy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.addPlayer("happy", "elf"); 
    player1.printTable(); 

    //Call WINPAUSE to pause console window before exiting 
    WINPAUSE; 
    return 0; 
} 

classe (Player.cpp)

#include <cstdlib> 
#include <iostream> 
#include <string> 

#include "Player.h" 

using namespace std; 


//Player constructor sets default values for the attributes 
//of the player 
Player::Player() 
{ 
    for (int i = 0; i < tableSize; i++) 
    { 
     HashTable[i] = new player; 
     HashTable[i]->name = "empty"; 
     HashTable[i]->race = "empty"; 
     HashTable[i]->next = NULL; 
    } 
} 

void Player::addPlayer(string name, string race) 
{ 
    int index = hash(name); 
    //Check to see if the attributes have been set at index 
    if (HashTable[index]->name == "empty") 
    { 
     HashTable[index]->name == name; 
     HashTable[index]->race == race; 
    } 

    //If they've already been set, then make an addition to the list 
    else 
    { 
     player* ptr = HashTable[index]; 
     player* newPlayer = new player; 
     newPlayer->name = name; 
     newPlayer->race = race; 
     newPlayer->next = NULL; 
     //Traverse to the end of the list 
     while (ptr->next != NULL) 
     { 
      ptr = ptr->next; 
     } 
     //Links the last item in the list to the new player being added 
     //New player now sits at the end of the list 
     ptr->next = newPlayer; 
    } 
} 

//Function to count the number of players 
int Player::numPlayersInIndex(int index) 
{ 
    //Sentinal variable to keep track of positon in list 
    int count = 0; 

    //If the index is empty, or has default value, return 0 to mark 
    //as empty. 
    if (HashTable[index]->name == "empty") 
    { 
     return count; 
    } 

    //Otherwise, increment count and then traverse the list pointed to 
    //in the hash table. 
    else 
    { 
     count++; 
     player* ptr = HashTable[index]; 
     while (ptr->next != NULL) 
     { 
      count++; 
      ptr = ptr->next; 
     } 
    } 
    return count; 
} 

void Player::printTable() 
{ 
    //Holds number of elements in each bucket 
    int number; 
    for (int i = 0; i < tableSize; i++) 
    { 
     //Sets number to the number of players in the hash table 
     number = numPlayersInIndex(i); 
     cout << "-------------" << endl; 
     cout << "index = " << i << endl; 
     cout << HashTable[i]->name << endl; 
     cout << HashTable[i]->race << endl; 
     cout << "Number of characters: " << number << endl; 
     cout << "-------------" << endl; 
    } 
} 

//Recursive function to create simple and fairly unique hash pattern 
//to process objects 
int Player::hash(string key) 
{ 
    int hash = 0; 
    int index; 

    for (int i = 0; i < key.length(); i++) 
    { 
     hash = hash + (int)key[i]; 
    } 

    index = hash % tableSize; 

    return index; 
} 

Répondre

0
HashTable[index]->name == name; 
    HashTable[index]->race == race; 

changer i t à

HashTable[index]->name = name; 
    HashTable[index]->race = race; 
+0

Ah, merci beaucoup! Je viens de tester et cela ajoute seulement le 6ème élément. Des pensées? – user3352763

+0

ce n'est pas le 6ème élément mais le 6ème indice. – d9ngle

+0

votre table de hachage a 10 emplacements, si deux clés hachent le même emplacement (exemple: 6% 10 = 6, 16% 10 = 6), vous utiliserez une liste liée pour les stocker. – d9ngle