用数组模拟栈
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
const int N = 1e6 + 10;
int stk[N], tt;//栈顶指针
//初始化
int index()
{
tt = -1;
}
//进栈
void push(int x)
{
stk[++tt] = x;
}
//出栈
void pop()
{
tt--;
}
//判断是否为空
void empty()
{
if (tt == -1)cout << "YES" << endl;
else cout << "NO" << endl;
}
//访问栈顶元素
void query()
{
cout << stk[tt]<<endl;
}
int main()
{
int m;
cin >> m;
index();
for (int i = 1; i <= m; i++)
{
string a;
cin >> a;
if (a == "push")
{
int x;
cin >> x;
push(x);
}
else if (a == "query")
{
query();
}
else if (a == "pop")
{
pop();
}
else if (a == "empty")
{
empty();
}
}
return 0;
}
不能这样写
string x;
scanf("%s",&x);