[AcWing 828] 模拟栈


点击查看代码
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int s[N], idx = 0;
void push(int x)
{
s[idx] = x;
idx ++;
}
void pop()
{
idx --;
}
bool empty()
{
return idx == 0;
}
int query()
{
return s[idx - 1];
}
int main()
{
int m;
scanf("%d", &m);
while (m --) {
string str;
cin >> str;
if (str == "push") {
int x;
cin >> x;
push(x);
}
if (str == "query") {
printf("%d\n", query());
}
if (str == "pop") {
pop();
}
if (str == "empty") {
if (empty()) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
- 使用数组模拟栈

浙公网安备 33010602011771号