2011-05-13 101 views
0

我想爲類編寫一個哈希表,我似乎無法得到這個while循環工作。你們看到有什麼問題嗎? while循環過早結束,我認爲我的while循環條件有問題嗎?在哈希表中搜索幫助!

void search(store t[], string s, int num, int table_size) 
{ 
    int temp = num; 
    bool exit = false; 
    while(t[temp].data != s && !exit){ 
    temp++; 
    if (temp == table_size){ 
     cout<<"reached 0 inside while loop"<<endl; 
     temp = 0; 
    } 
    if (temp == num){ 
     cout<<"test search loop"<<endl;   //I can't seem to get into here. 
     exit = true; 
    } 
    } 
    if(t[num].data == s) 
    cout<<"("<<s<<")"<<" appears "<<t[num].count<<" times."<<endl; 
    else 
    cout<<"your string is not in my table"<<endl; 
} 
+2

請舉例說明使用情況。你怎麼調用這個函數?你爲num,table_size傳遞了什麼值? – 2011-05-13 22:36:03

回答

1

while循環似乎確定,

,但是你確定下面的行?

if(t[num].data == s) 
    cout<<"("<<s<<")"<<" appears "<<t[num].count<<" times."<<endl; 

應該不是下面的呢?

if(t[temp].data == s) 
    cout<<"("<<s<<")"<<" appears "<<t[temp].count<<" times."<<endl; 

因爲很明顯,你正在運行的整個表,直到週期結束或直到您找到一個好[即:(!噸[臨時]。數據= S ...),而。所以我猜你正在尋找好的臨時索引,但你不在while循環之後使用它。

0

嘗試改變

int temp = num; 

int temp = 0;