题解 | #【模板】队列#
【模板】队列
https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); List<Integer> queue = new ArrayList<>(); in.nextLine(); for(int i = 0;i < a; i++){ String input = in.nextLine(); if(input.startsWith("push")){ String[] operate = input.split(" "); queue.add(0,Integer.parseInt(operate[1])); }else if(input.startsWith("pop")){ if(queue.isEmpty()){ System.out.println("error"); }else{ System.out.println(queue.get(queue.size()-1)); queue.remove(queue.size()-1); } }else{ if(queue.isEmpty()){ System.out.println("error"); }else{ System.out.println(queue.get(queue.size()-1)); } } } } }