AcWing 828. 模拟栈
原题链接
简单
作者:
microboat
,
2024-03-15 11:44:24
,
所有人可见
,
阅读 9
import java.io.*;
public class Main {
static int N = 100010;
static int[] stk = new int[N];
static int tt = -1;
public static void push(int x) {
stk[++tt] = x;
}
public static void pop() {
tt--;
}
public static boolean empty() {
return tt < 0;
}
public static int query() {
return stk[tt];
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int m = Integer.parseInt(br.readLine());
while (m-- > 0) {
String[] line = br.readLine().split(" ");
String op = line[0];
int x;
if (op.equals("push")) {
x = Integer.parseInt(line[1]);
push(x);
} else if (op.equals("pop")) {
pop();
} else if (op.equals("empty")) {
if (empty()) {
System.out.println("YES");
} else {
System.out.println("NO");
}
} else {
System.out.println(query());
}
}
}
}