1、用模板库实现顺序栈
#include <iostream>
#define Max 7
using namespace std;
template <typename T>
class Person
{
T top;
T data[Max];
public:
Person():top(-1),data{0}{}
void personpush(T s);
void personpopo();
bool empty();
bool full();
void show();
};
template <typename T>
bool Person<T>::empty()
{
return top==-1?1:0;
}
template <typename T>
bool Person<T>::full()
{
return top==Max-1?1:0;
}
template <typename T>
void Person<T>::personpush(T s)
{
if(full())
{
cout << "满栈" << endl;
return;
}
this->top++;
data[top]=s;
cout << data[top]<< "入栈" << endl;
return;
}
template <typename T>
void Person<T>::show()
{
if(empty())
{
cout << "空栈" << endl;
return;
}
for(int i=top;i>=0;i--)
{
cout << data[i] << " ";
}
cout << endl;
}
template <typename T>
void Person<T>::personpopo()
{
this->top--;
cout << data[top] << "出栈" << endl;
}
int main()
{
Person <int>s;
s.show();
s.personpush(8);
s.personpush(7);
s.personpush(6);
s.personpush(5);
s.personpush(4);
s.personpush(3);
s.personpush(2);
s.personpush(1);
s.personpopo();
s.personpopo();
s.personpopo();
s.personpopo();
s.show();
return 0;