저는 C++에서 선형 및 2 차 프로빙 해시 테이블 구현 작업을하고 있습니다. Hash.cpp에서 작동하는 linearProb (int key) 및 quadProb 함수가 있습니다. main.hpp를 통해 별도로 호출하면 올바른 해시 테이블이 인쇄되지만 컴파일 할 때 선형 및 2 차 테이블의 결과를보고 싶습니다. C++ 인쇄 배열
이
내 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;
}
}
}
}
을 (quadProb이 비슷)와 나는이
같은 MAIN.CPP에서 호출하면 나는 또한 Hash.cppvoid 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";
}
에 인쇄()를 가지고있다
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();
}
}
내 프로빙이 작동하는 것을 볼 수 있지만 linearProb를 테스트하기 위해 quadProb를 주석 처리했음을 알 수 있습니다. 나는 동시에 두 테이블을 출력하고 싶다. 이를 위해 필자는 main에서 호출하는 대신 각 조사 함수에서 print()를 호출하려고 시도했습니다.
이것은 내가 시도한 것입니다. 나는
while(!file.eof()){
file >> key;
h.linearProb(key);
//h.quadProb(key);
}
file.close();
에()의 주요 변경 및 linearProb (INT 키)
void Hash::linearProb(int key){
int i, count = 0;
Hash h;
if(key == -1){
h.print();
exit(1);
}
}
에 추가 그러나 이것은 단지 [i]를하지 않고 0 ~ 9를 인쇄. 내가 print()에 들어갔을 때 나는 무엇을 테스트했을 때, 나는 값이 모두 -1 인 것을 보여 주었고, 이는 아무 것도 출력하지 못하게한다. 왜 이런 일이 일어나고 있는지 정말 혼란 스럽습니다. main()을 통해 print()를 호출했을 때 print()가 제대로 작동하지 않는 이유는 무엇입니까?
코드를 읽는 데 다소 혼란 스럽습니다. [최소, 완전하며 검증 가능한 예제] (http://stackoverflow.com/help/mcve)를 제공해주십시오. – Steeve
코드 삽입 : 형식이 지정된 코드를 삽입하고 모든 항목을 선택하고 {} 아이콘 (코드 샘플) – stefaanv