tj
2024-10-06 18:07:24
发布于:广东
2阅读
0回复
0点赞
#include<iostream>
#include<stack>
using namespace std;
stack<int> s;
string tian;
int main()
{
	int n,x;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>tian;
		if(tian=="push")
		{
			cin>>x;
			s.push(x);
		}
		else if(tian=="pop")
		{
			if(s.empty())
			{
				cout << "pop fail" << endl;
				
			}
			else
			{
				cout << "pop " << s.top() <<endl;
                s.pop();
			}
		}
		else if(tian=="top")
		{
			if(s.empty())
			{
				cout << "top fail" << endl;
			}
			else
			{
				cout << "top = " << s.top() << endl;
			}
		}
		else if(tian=="size")
		{
			cout<<"size = "<<s.size()<<endl;
		}
		else if(tian=="empty")
		{
			if(s.empty())
			{
				cout << "yes" << endl;
			}
			else
			{
				cout << "no" << endl;
			}
		}
	} 
}
这里空空如也



有帮助,赞一个