模拟栈操作
2023-11-05 10:38:42
发布于:广东
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int n, x;
cin >> n;
string c;
stack <int> s;
for(int i = 1; i <= n; ++i)
{
cin >> c;
if(c == "push")
{
cin >> x;
s.push(x);
}
else if(c == "pop")
{
if(!s.empty())
{
cout << "pop " << s.top() << endl;
s.pop();
}
else
{
cout << "pop fail" << endl;
}
}
else if(c == "top")
{
if(!s.empty())
{
cout << "top = " << s.top() << endl;
}
else
{
cout << "top fail" << endl;
}
}
else if(c == "size")
{
cout << "size = " << s.size() << endl;
}
else if(c == "empty")
{
if(!s.empty())
{
cout << "no" << endl;
}
else
{
cout << "yes" << endl;
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个