#include <iostream>
using namespace std;
#include "SeqStack.cpp"
int main()
{
SeqStack<int> S;
if (S.Empty())
cout << "栈为空\n";
else
cout << "栈非空\n";
cout << "对15和10执行入栈操作\n";
S.Push(15);
S.Push(10);
cout << "栈顶元素为:" << S.GetTop() << "\n";
cout << "执行一次出栈操作\n";
S.Pop();
cout << "栈顶元素为:" << S.GetTop() << "\n";
return 0;
}
#ifndef SeqStack_H
#define SeqStack_H
const int StackSize = 10;
template <class DataType>
class SeqStack
{
private:
DataType data[StackSize];
int top;
public:
SeqStack();
~SeqStack() {}
void Push(DataType x);
DataType Pop();
DataType GetTop();
int Empty();
};
#endif
#include "SeqStack.h"
template <class DataType>
SeqStack<DataType>::SeqStack()
{
top = -1;
}
template <class DataType>
void SeqStack<DataType>::Push(DataType x)
{
if (top == StackSize - 1)
throw "Overflow";
data[++top] = x;
}
template <class DataType>
DataType SeqStack<DataType>::Pop()
{
if (top == -1)
throw "Underflow";
return data[top--];
}
template <class DataType>
DataType SeqStack<DataType>::GetTop()
{
if (top == -1)
throw "Stack is empty.";
return data[top];
}
template <class DataType>
int SeqStack<DataType>::Empty()
{
return top == -1;
}