数组模拟栈
#include <iostream>
using namespace std;
const int N = 100010;
int m;
int stk[N], tt;
int main()
{
cin >> m;
while(m--)
{
string op;
int x;
cin >> op;
if(op == "push")
{
cin >> x;
stk[++tt] = x;
}
else if(op == "pop") --tt;
else if(op == "empty") cout << (tt ? "NO" : "YES") << endl;
else cout << stk[tt] << endl;
}
return 0;
}
数组模拟队列
#include <iostream>
using namespace std;
const int N = 100010;
int m;
int q[N], hh, tt = -1;
int main()
{
cin >> m;
while(m--)
{
string op;
int x;
cin >> op;
if(op == "push")
{
cin >> x;
q[++tt] = x;
}
else if(op == "pop") ++hh;
else if(op == "empty") cout << (hh <= tt ? "NO" : "YES") << endl;
else cout << q[hh] << endl;
}
return 0;
}