实验内容
1、自己确定结点的具体数据类型和问题规模:
建立一个顺序栈,实现栈的压栈和出栈操作。
实验步骤
1、依据实验内容分别说明实验程序中用到的数据类型的定义;
2、相关操作的算法表达;
3、完整程序;
4、总结、运行结果和分析。
5、总体收获和不足,疑问等。
#include<iostream>
using namespace std;
const int StackSize = 10;
template<class DataType>
class SeqStack
{
private:
DataType data[StackSize];
int top;
public:
SeqStack() { top = -1; }
~SeqStack() {}
void Push(DataType x);
DataType Pop();
DataType GetTop() { if (top != -1) return data[top]; }
int Empty()
{
if (top == -1) return 1;
else return 0;
}
};
template <class DataType>
void SeqStack<DataType>::Push(DataType x)
{
if (top == StackSize - 1) throw"上溢";
data[++top] = x;
}
template<class DataType>
DataType SeqStack<DataType>::Pop()
{
if (top == -1) throw"下溢";
DataType x = data[top--];
return x;
}
int main()
{
SeqStack<int>stu;
stu.Push(11);
stu.Push(22);
stu.Push(33);
cout<<stu.GetTop()<<endl;
stu.Pop();
cout << stu.GetTop() << endl;
cout << stu.Empty() << endl;
stu.Pop();
stu.Pop();
cout << stu.Empty() << endl;
}