模拟栈操作
2024-02-17 17:20:07
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
int main(){
	stack<int>s;
	int n;
	cin>>n;
	for(int i = 0;i<n;i++){
		string a;
		cin>>a;
		if(a == "empty"){
			if(s.empty()){
				cout<<"yes"<<endl;
			}
			else{
				cout<<"no"<<endl;
			}
		}
		else if(a == "push"){
			int n;
			cin>>n;
			s.push(n);
		}
		else if(a == "size"){
			cout<<"size = "<<s.size()<<endl;
		}
		else if(a == "top"){
			if(s.empty()){
				cout<<"top fail"<<endl;
			}
			else{
				cout<<"top = "<<s.top()<<endl;
			}
		}
		else if(a == "pop"){
			if(s.empty()){
				cout<<"pop fail"<<endl;
			}
			else{
				cout<<"pop "<<s.top()<<endl;
				s.pop();
			}
		}
	}
	return 0;
}
这里空空如也






有帮助,赞一个