题解
2024-06-20 17:58:11
发布于:广东
223阅读
0回复
0点赞
#include<iostream>
using namespace std;
int st[105];
int top=0;
void push(int x){ 
	top++;
	st[top]=x;
}
void pop(){ 
	top--;
}
int TOP(){
	return st[top];
}
int size(){
	return top;
}
bool empty(){
	return top==0;
}
int main(){ 
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		string zl;
		cin>>zl;
		if(zl=="push"){
			int x;
			cin>>x;
			push(x);
		}
		else if(zl=="pop"){
			if(empty())
				cout<<"pop fail"<<endl;
			else{
				cout<<"pop "<< TOP()<<endl;
				pop();
			}
				
		}
		else if(zl=="top"){
			if(empty())
				cout<<"top fail"<<endl;
			else
				cout<<"top = "<<TOP()<<endl;
		}
		else if(zl=="size")
			cout<<"size = "<<size()<<endl;
		else if(zl=="empty"){
			if(empty())
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}
	return 0;
}
这里空空如也

有帮助,赞一个