#include<iostream.h> class stack { private: int count,maxsize; char *elem; public: stack(int size) { maxsize=size; count=0; elem=new char[maxsize]; } ~stack() { delete [maxsize]elem; } void push(char &e) { if(count==maxsize) cout<<"栈已满,无法入栈"<<endl; else { elem[count++]=e; } } bool empty() { return count==0; } int length() { return count; } void pop(char &e) { if(count==0) cout<<"栈为空,无法出栈"<<endl; else { e=elem[count-1]; count--; } } }; void main() { int size; cout<<"请输入栈的最大空间"<<endl; cin>>size; stack stack1(size); char ch='0',ch1,e; cout<<"请输入元素,以0结束"<<endl; while(1) { cin>>ch1; if(ch1==ch) break; else stack1.push(ch1); } cout<<"此栈的长度为"<<stack1.length()<<endl; cout<<"此栈的出栈顺序为"<<endl; while(1) { if(stack1.length()==0) break; else stack1.pop(e); cout<<e<<endl; } }