堆栈

这位更是重量级

给定一个初始为空的栈,执行下面几种操作n次:

1 x :表示在栈顶添加数字x x(1 ≤ x ≤ 100)

2 :输出栈顶的数字。

3 :删除栈顶的数字(保证此时栈不为空)。

4 :输出栈中含有的数字个数。

#include <iostream>

#include <stack>

using namespace std; ok无需多言

int main() {

int n;

cin >> n;无需多言

stack<int> st;芝士栈空间 存东西

for (int i = 0; i < n; ++i)又是循环 {

int operation;定义

cin >> operation;

下面开始读取编号进行糙做

if (operation == 1) {

int x;

cin >> x;

st.push(x); 狠狠加入x😋😋😋😋😋😋

} else if (operation == 2) {

if (!st.empty()) 栈顶 滚出去!!!!! {

cout << st.top() << endl;

}

} else if (operation == 3) {

if (!st.empty()) {

st.pop();直接删除

}

} else if (operation == 4) {

cout << st.size() << endl;

}也是权布输出了牢底🤣🤣🤣🤣🤣

}

return 0;

}

#你都收到了哪些公司的感谢信?##牛客创作赏金赛##软件开发薪资爆料##机械制造笔面经##23届找工作求助阵地#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务