#include<iostream>
using namespace std;
/// 定义栈的大小
const int n = 10;
template <class T>
/// 定义类
class Stack
{
///T为元素类型,栈大小为10
T sk[n];
/// 栈顶元素位置
int top;
public:
/// 构造函数 用于初始化栈顶
Stack()
{
top = -1;
}
///入栈函数T为参数类型
void push(T ob);
/// 出栈函数T为返回值类型
T pop();
};
template<class T>
///入栈函数T为参数类型
void Stack<T>::push(T ob)
{
///栈第一个空间标识为0 n个空间最上面的空间为n-1
if (top == n - 1)
{
cout << "stack is full";
return;
}
///不满则栈顶网上挪且把要入栈的元素装入
sk[++top] = ob;
}
template<class T>
T Stack<T>::pop()
{
/// 判断栈是否为空
if (top < 0)
{
cout << "Stack is empty";
return (0);
}
/// 不为空的话,栈顶往下移上面的元素就没有了
return sk[top--];
}
int main()
{
Stack<char> s1;
Stack<int>s2, * sp = &s2;
s1.push('a');
s1.push('b');
s1.push('c');
cout << "pop s1:";
for (int i = 0; i < 3; i++)
cout << s1.pop();
s2.push(1);
s2.push(2);
s2.push(3);
cout << endl << "pop s2:";
for (int i = 0; i < 3; i++)
cout << sp->pop();
cout << endl;
return 0;
}