[AcWing]828. 模拟栈(C++实现)栈模板题
1. 题目
2. 读题(需要重点注意的东西)
思路:用数组来实现一个栈stk[N],用tt指向栈顶
3. 解法
---------------------------------------------------解法---------------------------------------------------
#include<iostream>
using namespace std;
const int N = 1e5+10;
int stk[N],tt;
int main(){
int m;
cin >> m;
while(m--){
string op ;
cin >> op;
if(op == "push"){
int x;
cin >> x;
stk[++tt] = x;
}
if(op == "pop"){
--tt;
}
if(op == "query"){
cout << stk[tt] << endl;
}
if(op == "empty") cout << (tt ? "NO" : "YES") << endl;
}
return 0;
}
4. 可能有帮助的前置习题
5. 所用到的数据结构与算法思想
6. 总结
栈用数组实现的模板题,推荐完全背下来。