题解 | #【模板】循环队列#
【模板】循环队列
https://www.nowcoder.com/practice/0a3a216e50004d8bb5da43ad38bcfcbf
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayDeque; import java.util.Deque; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); String[] array = br.readLine().split(" "); int capacity = Integer.parseInt(array[0]); int len = Integer.parseInt(array[1]); Deque<String> dq = new ArrayDeque<>(capacity); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { String str = br.readLine(); if (str.equals("pop")) { if (dq.isEmpty()) { sb.append("empty").append("\n"); continue; } sb.append(dq.remove()).append("\n"); } else if (str.equals("front")) { if (dq.isEmpty()) { sb.append("empty").append("\n"); continue; } sb.append(dq.element()).append("\n"); } else { if (dq.size() == capacity) { sb.append("full").append("\n"); continue; } dq.add(str.substring(5)); } } System.out.println(sb); } }