#include <iostream>using namespace std;class My_stack
{
private:int *ptr; //指向堆区空间int top; //记录栈顶元素int size;
public://有参构造My_stack(int size):ptr(new int[size]),top(-1){this->size=size;cout<<"My_stack::有参构造"<<endl;}//析构函数~My_stack(){delete []ptr;this->ptr=nullptr;cout<<"My_stack::析构构造"<<endl;}//判满函数bool my_full(){if(this->top==size-1){return 1;}elsereturn 0;}//判空函数bool my_empty(){if(this->top==-1){return 1;}elsereturn 0;}//入栈函数int push(int c){if(my_full())return -1;ptr[++top]=c;return 0;}//出栈函数int output(){if(my_empty())return -1;top--;return 0;}//遍历栈void put(){if(my_empty())return ;for(int i=0;i<=top;i++){cout<<ptr[i]<<" ";}cout<<endl;}//获取栈顶元素的引用void my_top(){cout<<ptr[top]<<endl;}};int main()
{My_stack s(5);int c;for(int i=0;i<5;i++){cout<<"请输入: "<<endl;cin>>c;getchar();s.push(c);}s.put();s.my_top();return 0;}