//
// Created by 刘彪 on 2020/3/1.
//栈类(类模板)P178
#include <iostream>
#include <stdlib.h>
#include <cstdio>
using namespace std;
const int Max = 20;
template <class T>
class Stack{
T s[Max];
int top;
public:
Stack(){
top = -1;
}
void push (const T &item);
T pop();
int Stackempty() const;
};
template <class T>
void Stack<T>::push(const T &item) {
if(top == Max-1){
cout << "栈满溢出"<<endl;
exit(1);
}
top++;
s[top] = item;
}
template <class T>
T Stack<T>::pop() {
T temp;
if(top == -1){
cout << "栈为空,不能出栈操作"<<endl;
exit(1);
}
temp = s[top];
top--;
return temp;
}
template <class T>
int Stack<T>::Stackempty() const {
return top==-1;
}
int main(){
Stack <int> st1;
int a[] = {4,8,3,2};
cout<<"整数栈"<<endl;
cout<<" 进栈序列"<<endl;
for(int i=0;i<4;i++){
cout<<a[i]<<" ";
st1.push(a[i]);
}
cout<<endl<<" 出栈序列:";
while(!st1.Stackempty()) cout<<st1.pop()<<" ";
cout<<endl;
cout<<"字符栈"<<endl;
Stack <char> st2;
char b[] = {'a','d','b','c'};
cout<<" 进栈序列"<<endl;
for(int i=0;i<4;i++){
cout<<b[i]<<" ";
st2.push(b[i]);
}
cout<<endl<<" 出栈序列";
while(!st2.Stackempty()) cout<<st2.pop()<<" ";
cout<<endl;
return 0;
}