题目描述
模拟栈
JAVA 代码
import java.util.*;
import java.io.*;
class Main{
static final int N = 100010;
static int[] stk = new int[N];
static int tt = 0;
public static void main(String args[])throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int m = Integer.parseInt(in.readLine());
while(m-->0){
String[] s = in.readLine().split(" ");
String c = s[0];
if(c.equals("push")){
int x = Integer.parseInt(s[1]);
//尾指针 先自增 再入栈
stk[++tt] = x;
}else if(c.equals("pop")){
//尾指针自减
tt--;
}else if(c.equals("query")){
//栈顶为 尾指针的值.
System.out.println(stk[tt]);
}else{
//尾指针不为0 则不为空
if(tt>0) System.out.println("NO");
else System.out.println("YES");
}
}
}
}