#include <iostream>
#define MAXSIZE 100
using namespace std;
template <typename T>
class stacklink
{
private:
T data[MAXSIZE]; //数据元素
int top = -1; //栈顶
public:
stacklink() {}
stacklink(T e):data(e){}
~stacklink(){}
//判空
bool empty()
{
return (top == -1); //栈空则返回1
}
//判满
bool full()
{
return (top == MAXSIZE-1); //栈满则返回1
}
//入栈
T push(T e)
{
if(1 == full())
{
cout << "栈满..." <<endl;
return -1;
}
this->top++;
this->data[this->top] = e;
return 0;
}
//出栈
T pop()
{
if(1 == empty())
{
cout << "栈空..." <<endl;
return -1;
}
cout << "出栈元素为: " << this->data[this->top] <<endl;
this->top--;
return 0;
}
//栈的遍历
void output();
};
//栈的遍历
template <typename T>
void stacklink<T>::output()
{
for(int i=0;i<=this->top;i++)
{
cout << this->data[i] <<" ";
}
cout <<endl;
}
int main()
{
stacklink<int> s;
//进栈
for(int i=0;i<10;i++)
{
s.push(i);
}
cout<<"栈中元素:";
//遍历
s.output();
//出栈
s.pop();
s.pop();
cout<<"栈中剩余元素:";
s.output();
return 0;
}
运行结果: