誰でも私を助けることができますか?hashSize が小さい場合、printAll()、listprintAll()、および sizeLL() は正しく機能しますが、9973 のような大きな数では機能しませんでした。
printAll() と hashStats() はどちらもクラス テーブルのメソッドであり、printALL() は listprintAll() を呼び出し、hashStats() は別の構造体から sizeLL() を呼び出します。
すべての関数は、小さい hashSize を指定すると正しく機能します。
写真が乱れて申し訳ありません。ここで初めて..私はこの作業を行うために MacBook を使用しています。
list.h 内
struct Node{
string key;
int value;
Node *next;
Node(const string &theKey, int theValue);
Node(const string &theKey, int theValue, Node *n);
};
typedef Node * ListType;
Table.h で
class Table {
public:
static const int HASH_SIZE = 9973; // a prime number
// create an empty table, i.e., one where numEntries() is 0
// (Underlying hash table is HASH_SIZE.)
Table();
// create an empty table, i.e., one where numEntries() is 0
// such that the underlying hash table is hSize
Table(unsigned int hSize);
unsigned int hashSize; // size of the hash table
ListType * data; // (used in hashCode method above)
}
list.cpp 内
void listprintAll(ListType list){
if(list ==NULL) {
cout << "[ ]" <<endl;
return;}
else{
Node * p=list;
while(p!= NULL){
cout << p->key << " " << p->value << ",";
p=p->next;
}
cout <<endl;
return;
}
}
int sizeLL(ListType list){
if(list ==NULL) {
return 0;}
else{
int count=0;
Node * p=list;
while(p!= NULL){
p=p->next;
count++;
}
return count;
}
Table.cpp 内
Table::Table() {
hashSize=HASH_SIZE;
data = new ListType[hashSize];
}
Table::Table(unsigned int hSize) {
hashSize=hSize;
data = new ListType[hashSize];
}
void Table::printAll() const {
for(int i=0;i<hashSize;i++){
listprintAll(data[i]);
}
}
void Table::hashStats(ostream &out) const {
out << "number of buckets: "<< hashSize <<endl;
int number = numEntriesOfTable();
out << "number of entries: "<< number <<endl;
int countN0=0;
int longest=0;
int temp;
if(number!=0){
for(int i=0;i<hashSize;i++){
temp=sizeLL(data[i]);
if(temp!=0){
countN0++;
if(temp > longest){
longest=temp;
}
}
}
}
out << "number of non-empty buckets: "<< countN0 << endl;
out << "longest chain : "<< longest << endl;
}