题解 | #【模板】队列#
【模板】队列
https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549
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) ); StringBuilder sb = new StringBuilder(); int size = Integer.parseInt(br.readLine()); Deque<String> queue = new ArrayDeque<>(); for (int i = 0; i < size; i++) { String str = br.readLine(); if (str.equals("pop")) { if (queue.isEmpty()) { sb.append("error").append("\n"); continue; } sb.append(queue.remove()).append("\n"); } else if (str.equals("front")) { if (queue.isEmpty()) { sb.append("error").append("\n"); continue; } sb.append(queue.element()).append("\n"); } else { String str1 = str.substring(5); queue.add(str1); } } System.out.println(sb); } }