思路
- 栈是先进后出
- 其实就是实现4个函数
- 需要一个数组,和一个指向栈顶的指针
心得
- 上学期学过数据结构,所以还好理解
- 但是不太会怎么把思路写下来
代码
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100010;
int s[N], t;
void push(int x)
{
s[++ t] = x;
}
void pop()
{
t --;
}
void empty()
{
if(!t) puts("YES");
else puts("NO");
}
void query()
{
cout << s[t] << endl;
}
int main()
{
string op;
int n; cin >> n;
while(n --)
{
cin >> op;
if(op == "push")
{
int x;
scanf("%d", &x);
push(x);
}
else if(op == "pop")
{
pop();
}
else if(op == "empty")
{
empty();
}
else query();
}
return 0;
}