2016-12-02 14 views
-1

Ich arbeite an lineare und quadratische Probing-Hash-Tabelle-Implementierung in C++. In Hash.cpp habe ich eine funktionierende linearProb (int key) und quadProb Funktionen. Wenn ich sie separat über main.hpp aufruft, druckt sie die korrekte Hash-Tabelle aus, aber ich möchte das Ergebnis sowohl der linearen als auch der quadratischen Tabellen sehen, wenn ich kompiliere.C++ - Druck-Array

Das ist mein linearProb

void Hash::linearProb(int key){ 

    int i, count = 0; 
    Hash h; 

    //if it is empty, place it there 
    if (a[key % tableSize] == -1) 
    a[key % tableSize] = key; 

    else{ 
     i = 0; 

     //until finding an empty slot, but don't loop around 
     while (i < tableSize && a[i] != -1){ 
       count++; 
       i++; 
     } 

     if(count == tableSize){ 
      cout<<key<<" could not be inserted in the table\n"; 
      exit(1); 
     } 
     //when there's a collision increase i by 1 until finding empty slot 
     for(i = (key % tableSize+1) % tableSize; i <tableSize; i++){ 
      if(a[i] == -1){ 
       a[i] = key; 
       break; 
      } 
     } 
    } 
} 

und ich habe auch drucken (quadProb ähnlich sieht)() in Hash.cpp

void Hash::print(){ 
    int i; 

    //cout<<"Hash Table with Linear Probing"<<endl; 
    cout<<"\n Result Hash Table: "<<endl; 

    for(i = 0; i < tableSize; i++){ 
     cout<<"\n"<<i; 
     if(a[i] != -1){ 
     cout<<" "<< a[i]; 
     } 
    } 
    cout<<"\n"; 
} 

Wenn ich es in main.cpp wie dieses

nennen
int main(){ 
    int key; 
    Hash h; 

    //take in .txt file 
    std::fstream file; 
    file.open("keys.txt"); 

    while(!file.eof()){ 
     file >> key; 

     if(key != -1){ 
     h.linearProb(key); 
     //h.quadProb(key); 
     } 
    } 

    file.close(); 

    if(key == -1){ 
     h.print(); 
    } 
} 

Ich kann sehen, dass meine Sondierung funktioniert, aber beachten Sie, dass ich quadProb auskommentiert, um linearProb zu testen. Ich möchte beide Tabellen gleichzeitig ausdrucken. Um dies zu tun, habe ich versucht, print() in jeder Suchfunktion aufzurufen, anstatt sie von main aus aufzurufen.

Das habe ich ausprobiert. Ich änderte main() zu

while(!file.eof()){ 
    file >> key; 

    h.linearProb(key); 
    //h.quadProb(key);   
} 

file.close(); 

und zu linearProb (int Schlüssel)

void Hash::linearProb(int key){ 
    int i, count = 0; 
    Hash h; 

    if(key == -1){ 
     h.print(); 
     exit(1); 
    } 
} 

Aber dies nur aus 0 ~ 9 ohne [i] drucken. Wenn ich getestet habe, was a [i] ist, wenn es in print() eintritt, und es mir alles gibt, hat ich einen Wert von -1, was dazu führt, dass nichts gedruckt wird. Ich bin wirklich verwirrt, warum das passiert. Warum wird print() nicht korrekt a [i], obwohl es funktionierte, als ich print() über main anrief?

+0

Ich bin ein wenig verwirrt, Ihren Code zu lesen, können Sie bitte ein [minimales, vollständiges und überprüfbares Beispiel] (http://stackoverflow.com/help/mcve) zur Verfügung stellen? – Steeve

+0

Beim Einfügen von Code: Formatierten Code einfügen, alles auswählen und das Symbol {} drücken (Codebeispiel) – stefaanv

Antwort

2

In Ihrer "Drucken von Probe-Funktion", drucken Sie einen leeren Hash-h in der Funktion deklariert. Sie sollten diese Hash h; fallen lassen und print() anstelle von h.print() anrufen.

Dies ist ein nettes Problem, mit dem ein Debugger Ihnen helfen kann. Wenn Sie in diesem Zweig brechen, würde es eine leere h zeigen, während in Haupt, h würde gefüllt werden.