具体过程见代码
代码
import java.io.*;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static final int N = 100010;
static int[] q = new int[N]; // 数据规模为 10w
static int hh = 0, tt =-1; // hh指向队头, tt指向队尾
// 入队
public static void push(int val) {
q[++tt] = val;
}
// 出队
public static void pop() {
hh++;
}
// 判断队列是否为空
public static boolean empty() {
return hh > tt;
}
// 查询队头元素
public static int query() {
return q[hh];
}
// 查询队尾元素
public static int queryToTail() {
return q[tt];
}
// 程序入口
public static void main(String[] args) throws IOException {
int m = Integer.parseInt(reader.readLine());
while (m-- > 0) {
String[] s = reader.readLine().split(" ");
if (s[0].equals("push")) {
push(Integer.parseInt(s[1]));
} else if (s[0].equals("pop")) {
pop();
} else if (s[0].equals("query")) {
System.out.println(query());
} else { // s[0].equals("empty")
if (empty()) System.out.println("YES");
else System.out.println("NO");
}
}
}
}