C++顺序栈的模板实现
#pragma once
//顺序栈的实现
#include <iostream>
using namespace std;
const int StackSize = 10;
template<typename DataType>
class SeqStack
{
public:
SeqStack(){ top = -1; }
~SeqStack(){}
void Push(DataType x);
DataType Pop();
DataType GetTop()
{
if (top != -1)
{
return data[top];
}
}
int Empty(){ top == -1 ? return 1 : return 0; }
private:
DataType data[StackSize];
int top;
};
template<typename DataType>
void SeqStack<DataType>::Push (DataType x)
{
if (top == StackSize - 1)
{
throw"上溢";
}
data[++top] = x;
}
template<typename DataType>
DataType SeqStack<DataType>::Pop ()
{
if (top == -1)
{
throw"下溢";
}
x = data[top--];
return x;
}