【发布时间】:2020-03-14 04:00:49
【问题描述】:
所以我一直在尝试搜索用户输入的数字是否存在于队列中,如果存在则该函数将返回 true,否则返回 false。但是,无论我尝试什么方法,它都不起作用:
bool cqueue::search(int x){ //this is my class cqueue
if(empty()) { cout<<"\n\n queue is empty\n"; }
for(int i=0;i<size;i++){
if(array[i]==x){
return true; cout<<x<<" is in the queue \n"; break; }
else {
return false; cout<<x<<" is not in the queue \n"; }
}
}
【问题讨论】:
-
遵循你的逻辑。如果第一个元素与您的搜索不匹配会怎样?
-
此外,如果您的队列为空,则不会返回任何内容。确保您返回一些东西。