Quelqu'un peut-il m'aider? Les fonctions printAll(), listprintAll() et sizeLL() fonctionnent correctement lorsque hashSize est petit mais ne fonctionne pas avec un grand nombre, comme le numéro 9973.Erreur de bus C++ Exécuter correctement avec un petit tableau, erreur d'exécution avec un grand tableau
printAll() et hashStats() sont toutes les deux méthodes dans Class Table, printALL() appelle listprintAll() et hashStats() appelle sizeLL() d'une autre structure.
Toutes les fonctions fonctionnent correctement avec un petit hashSize.
Désolé pour l'image et confus. première fois ici .. J'utilise MacBook pour faire ce travail.
Dans 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;
Dans 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)
}
Dans 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;
}
Dans 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;
}
Bienvenue sur SO! Vous devriez publier le code comme du texte, pas comme une image, et être plus spécifique – yuyoyuppe
Vraiment une erreur de bus? Quel système utilisez-vous? Une machine 68K ou quelque chose? – BitTickler