在堆栈中查找一个项目
在我的代码中,我试图找到放入堆栈的特定项目。为此,我将所有项目移至临时堆栈,将其从原始堆栈中弹出。弹出后,我要将所有项目按原始顺序移回到原始堆栈中。我的代码永远无法识别该项目在堆栈中,因此当它实际上在堆栈中时我发现它没有找到。你能帮我调试我的循环吗?
int main:
#include <iostream>
#include "Stack.h"
#include "Gumball.h"
using namespace std;
int main()
{
Stack s, gumballStack;
Gumball g, temp;
char choice;
bool choice_flag = true;
do {
cin >> choice;
cin >> g.color;
switch(choice)
{
case 'b':
case 'B':
cout << "A" << " " << g.color << " gumball has been bought." << endl << endl;
g.counter = 0;
s.isempty();
s.push(g);
if(!s.isfull())
cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl;
else
cout << "There is no room for another gumball." << endl << endl;
break;
case 'e':
case 'E':
s.isempty();
temp = s.pop();
if(s.isempty() && temp.color == g.color)
{
cout << "The " << g.color << " gumball has been eaten." << endl << endl;
}
从这里开始,我相信是错误:
while(!s.isempty() && g.color != temp.color)
{
gumballStack.push(temp);
g.counter++;
s.pop();
cout << " " << temp.counter << endl << endl;
}
if(!s.isempty())
{
cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl;
}
else
{
cout << "The gumball cannot be found." << endl << endl;
}
while(!gumballStack.isempty())
{
//gumballStack.pop();
s.push(gumballStack.pop());
gumballStack.pop();
}
break;
case 'q':
case 'Q':
choice_flag = false;
break;
}
} while(choice_flag);
return 0;
}
.h 文件:
#ifndef STACK_H
#define STACK_H
#include "Gumball.h"
// Interface file - Stack class definition
class Stack {
public:
Stack();
void push(Gumball);
Gumball pop();
bool isempty();
bool isfull();
private:
Gumball gumballs[6+1];
int top;
};
#endif // STACK_H
回答你的问题 @TOM:
好吧,我认为 .cpp (用于 stack.h)是它将回答您提出的大多数问题:
#include "Stack.h"
#include "Gumball.h"
using namespace std;
// Constructor to initialize the stack
Stack::Stack()
{
top = -1;
}
// Function to add item x to stack
void Stack::push(Gumball x)
{
if(!isfull()){
top++;
gumballs[top] = x;
return; }
else
return;
}
// Function to remove and return top item of stack
Gumball Stack::pop()
{
Gumball x;
if(!isempty()) {
x = gumballs[top];
top--;
return x; }
else
return x;
}
// Function to check if stack is empty
bool Stack::isempty()
{
if (top == -1)
return true;
else
return false;
}
// Function to check if stack is full
bool Stack::isfull()
{
if (top == 6)
return true;
else
return false;
}
我看到您所说的问题,我多次将 temp 放回到堆栈中......绝对不是我的意图,谢谢您指出这一点。我如何才能将每个口香糖球添加到堆栈中,该口香糖球不等于我正在寻找的物品,而不是相同的物品?
我认为添加 Gumball.h 和 .cpp 将回答您的其他问题,所以这里是:
gumball.h 文件:
#ifndef GUMBALL_H
#define GUMBALL_H
#include <iostream>
using namespace std;
// Interface file - Gumball class definition
class Gumball
{
public:
Gumball();
string color;
int counter;
private:
};
#endif // GUMBALL_H
gumball.cpp 文件:
#include "Gumball.h"
Gumball::Gumball()
{
color = " ";
counter = 0;
}
In my code, I am trying to find a specific item put onto the stack. To do so, i am moving all the items in the way to a temp stack, to pop it off the original stack. After it is popped, I am to move all the items back in the original stack in the original order. My code never recognizes that the item was in the stack so I get it is not found when it is in fact in the stack. Can you please help me debug my loop ...
int main:
#include <iostream>
#include "Stack.h"
#include "Gumball.h"
using namespace std;
int main()
{
Stack s, gumballStack;
Gumball g, temp;
char choice;
bool choice_flag = true;
do {
cin >> choice;
cin >> g.color;
switch(choice)
{
case 'b':
case 'B':
cout << "A" << " " << g.color << " gumball has been bought." << endl << endl;
g.counter = 0;
s.isempty();
s.push(g);
if(!s.isfull())
cout << "The gumball is" << " " << g.color << " and has been stored." << endl << endl;
else
cout << "There is no room for another gumball." << endl << endl;
break;
case 'e':
case 'E':
s.isempty();
temp = s.pop();
if(s.isempty() && temp.color == g.color)
{
cout << "The " << g.color << " gumball has been eaten." << endl << endl;
}
From here on, i believe is the error:
while(!s.isempty() && g.color != temp.color)
{
gumballStack.push(temp);
g.counter++;
s.pop();
cout << " " << temp.counter << endl << endl;
}
if(!s.isempty())
{
cout << "The " << " " << g.color << " gumball has been eaten." << endl << endl;
}
else
{
cout << "The gumball cannot be found." << endl << endl;
}
while(!gumballStack.isempty())
{
//gumballStack.pop();
s.push(gumballStack.pop());
gumballStack.pop();
}
break;
case 'q':
case 'Q':
choice_flag = false;
break;
}
} while(choice_flag);
return 0;
}
The .h file:
#ifndef STACK_H
#define STACK_H
#include "Gumball.h"
// Interface file - Stack class definition
class Stack {
public:
Stack();
void push(Gumball);
Gumball pop();
bool isempty();
bool isfull();
private:
Gumball gumballs[6+1];
int top;
};
#endif // STACK_H
TO ANSWER YOUR QUESTION @TOM:
well the .cpp (for stack.h) is, I think it will answer most of the questions you asked:
#include "Stack.h"
#include "Gumball.h"
using namespace std;
// Constructor to initialize the stack
Stack::Stack()
{
top = -1;
}
// Function to add item x to stack
void Stack::push(Gumball x)
{
if(!isfull()){
top++;
gumballs[top] = x;
return; }
else
return;
}
// Function to remove and return top item of stack
Gumball Stack::pop()
{
Gumball x;
if(!isempty()) {
x = gumballs[top];
top--;
return x; }
else
return x;
}
// Function to check if stack is empty
bool Stack::isempty()
{
if (top == -1)
return true;
else
return false;
}
// Function to check if stack is full
bool Stack::isfull()
{
if (top == 6)
return true;
else
return false;
}
I see the problem you've stated, that i am putting temp back onto the stack multiple times... definitely not my intentions, thank you for pointing that out. How do i get it to add each gumball in the stack that is not equal to the item i am looking for ,instead of the same one?
I think adding the Gumball.h and .cpp will answer ur other questions so here it is:
gumball.h file:
#ifndef GUMBALL_H
#define GUMBALL_H
#include <iostream>
using namespace std;
// Interface file - Gumball class definition
class Gumball
{
public:
Gumball();
string color;
int counter;
private:
};
#endif // GUMBALL_H
gumball.cpp file:
#include "Gumball.h"
Gumball::Gumball()
{
color = " ";
counter = 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是,
但请注意,当吃掉的口香糖是堆栈中的最后一个时,此代码将不起作用,因为 s 将为空。
除了同意汤姆的观点并指出您考虑其他解决方案(例如 stl::list )之外,您应该使用类似的东西
It should be
but pay attention that this code will not work when eaten gumball is the last in the stack, because s will be empty.
Other than agreeing with tom and pointing you in thinking about other solutions (stl::list for example), you should use something like this
如果没有看到 Stack 的实现,很难说问题出在哪里。但是,由于我发现您的代码的几个部分令人困惑,因此我认为指出其中的位置可能对您有用。如果您更改代码的接口以使其更清晰,那么您的问题可能会变得明显。
在上述问题中,
isempty()
的常量性在接下来的内容中尤为重要。理想情况下,您将使用迭代器重写整个过程。看一下 std::find 的界面。
Its difficult to say what the problem is without seeing the implementation of
Stack
. However, since I found several parts of your code confusing, I thought it might be useful to you to indicate where. If you change the interface to your code so that it is clearer, it might be that your problems become apparent.Of the above questions, the
const
ness ofisempty()
is particularly important in what followsIdeally, you would rewrite the whole thing with iterators. Have a look at the interface for std::find.