tj
2024-08-09 09:34:08
发布于:四川
4阅读
0回复
0点赞
#include <iostream>
#include <stack>
using namespace std;
int main() {
int n;
cin >> n;
stack<int> st;
string s;
int x;
for (int i = 0; i < n; ++i) {
cin >> s;
if (s == "push") {
cin >> x;
st.push(x);
} else if (s == "empty") {
if (st.empty()) {
cout << "yes" << "\n";
} else {
cout << "no" << "\n";
}
}else if (s == "pop") {
if (!st.empty()) {
cout << "pop " << st.top() << "\n";
st.pop();
} else {
cout << "pop fail" << "\n";
}
} else if (s == "top") {
if (!st.empty()) {
cout << "top = " << st.top() << "\n";
} else {
cout << "top fail" << "\n";
}
} else if (s == "size") {
cout << "size = " << st.size() << "\n";
}
}
return 0;
}
这里空空如也
有帮助,赞一个