我是这个网站的新手,我会尽我所能做出贡献。另外,请知道我从来没有在不花很多时间自己弄清楚的情况下提出问题。
因此,C++ 堆栈让我发疯。
我的问题:我应该将我的变量/值放在 Stack 功能块中的什么位置以实际使用它。我知道 Stacks 是一种 LIFO 数据结构,我已经阅读了无数将板堆叠在一起的例子,等等。
看看这段代码:
#include <iostream>
using namespace std;
const int MAX_SIZE = 100;
class StackOverFlowException
{
public:
StackOverFlowException()
{
cout << "Stack overflow" << endl;
}
};
class StackUnderFlowException
{
public:
StackUnderFlowException()
{
cout << "Stack underflow" << endl;
}
};
class ArrayStack
{
private:
int data[MAX_SIZE];
int top;
public:
ArrayStack()
{
top = -1;
}
void Push(int element)
{
if ( top >= MAX_SIZE )
{
throw new StackOverFlowException();
}
data[++top] = element;
}
int Pop()
{
if ( top == -1 )
{
throw new StackUnderFlowException();
}
return data[top--];
}
int Top()
{
return data[top];
}
int Size()
{
return top + 1;
}
bool isEmpty()
{
return ( top == -1 ) ? true : false;
}
};
[ETC....]
这是基本的千篇一律....假设我正在尝试对其进行调整以表达一个系统,其中最后的食品订单首先被踢出;变量是“食物”、“订单”等等。我在哪里将这些变量集成到上面的堆栈代码中!?!??!
请帮助我很困惑我要不分青红皂白地打东西