1. #include<iostream>
    2. using namespace std;
    3. template<class T>
    4. class mystack{
    5. public:
    6. //数据成员的定义
    7. T a[10];
    8. T top;
    9. mystack(){
    10. top = -1;
    11. }
    12. //析构函数的定义
    13. ~mystack(){}
    14. //判断栈空
    15. bool isempty(){
    16. if(top == -1){
    17. return true;
    18. }
    19. return false;
    20. }
    21. //判断栈满
    22. bool isfull(){
    23. if(top == 9){
    24. return true;
    25. }
    26. return false;
    27. }
    28. //压栈操作函数定义
    29. void Push(T n){
    30. if(!isfull()){
    31. a[++top] = n;
    32. }
    33. else{
    34. cout<<"栈已满,无法入栈"<<endl;
    35. return;
    36. }
    37. }
    38. //出栈操作函数定义
    39. T Pop(){
    40. if(!isempty()){
    41. return a[top--];
    42. }
    43. else{
    44. cout<<"栈为空,无法出栈"<<endl;
    45. return 0;
    46. }
    47. }
    48. };
    49. int main(){
    50. mystack<int> zhan;
    51. zhan.Push(1);
    52. zhan.Push(2);
    53. zhan.Push(3);
    54. zhan.Push(4);
    55. zhan.Push(5);
    56. zhan.Push(6);
    57. zhan.Push(7);
    58. zhan.Push(8);
    59. zhan.Push(9);
    60. zhan.Push(10);
    61. zhan.Push(11);
    62. cout<<"出栈:"<<zhan.Pop()<<endl;
    63. for(int i = 0; i <= zhan.top; i++){
    64. cout<<zhan.a[i];
    65. }
    66. return 0;
    67. }

    image.png