谁能帮我?printAll()、listprintAll() 和 sizeLL() 在 hashSize 很小但不适用于大数字(如数字 9973)时正常工作。
printAll() 和 hashStats() 都是 Class Table 中的方法,printALL() 调用 listprintAll() 和 hashStats() 从另一个结构调用 sizeLL()。
所有函数都可以在给定小的 hashSize 的情况下正常工作。
抱歉图片和困惑。第一次来这里.. 我正在使用 MacBook 来完成这项工作。
在列表.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;
在表.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;
}