#include <iostream>
using namespace std;
#define MAX 10
class Stack{
public:
Stack();
void inStack(int n);
int outStack();
int getSize();
private:
int size;
int array[MAX];
};
Stack::Stack(){
size=0;
}
void Stack::inStack(int n){
if(size<MAX){
array[size]=n;
size++;
}
else{
cout<<"此栈已满!"<<endl;
}
}
int Stack::outStack(){
if(size<=0){
return -404;
}else
return array[--size];
}
int Stack::getSize(){
return size;
}
int main(){
Stack s;
int i;
s.inStack(1);
s.inStack(2);
s.inStack(3);
s.inStack(4);
s.inStack(5);
s.inStack(6);
s.inStack(7);
s.inStack(8);
s.inStack(9);
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.getSize();
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.getSize();
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.getSize();
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.outStack();
cout<<i<<endl;
i=s.getSize();
cout<<"还剩"<<i<<"个"<<endl;
return 0;
}
技术支持课【栈】
最新推荐文章于 2024-07-14 12:00:00 发布