开发者

Searching in a hash table HELP!

I am trying to write a hash table for class and I can't seem to get this while loop working. Do you guys see anything wrong with it? The while loop is ending prematurely, I think there is something wrong with my while loop condition?

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;
}


Your while loop seems ok,

but are you sure about the following line ?

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

shouldn't be the following instead ?

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

Because obviously you are running the whole table until the cycle is complete or until you find the good one [ ie: while( t[temp].data != s ...) ]. So I guess that you are searching for the good temp index but you don't use it after you while loop.


Try changing

 int temp = num;

to

 int temp = 0;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜