0
我想做一個簡單的哈希表項目。當我注意到addPlayer函數不工作時,我正在測試我的程序。 addPlayer依賴於其他一些函數,但我也檢查了這些函數,並且找不到錯誤。如果我能自己診斷這一點,我會非常感激這方面的任何幫助。 其全部的程序是如下:哈希表addFunction
頭文件(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.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;
}
類(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;
}
啊,非常感謝你!我剛剛測試過,它只是添加了第六個元素。有什麼想法嗎? – user3352763
它不是第6個元素,而是第6個索引。 – d9ngle
你的散列表有10個插槽,如果兩個鍵散列到同一個插槽(例如:6%10 = 6,16%10 = 6),你將使用鏈表來存儲它們。 – d9ngle