题解 | #【模板】队列#
【模板】队列
http://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { private Queue queue = new LinkedList<>();
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
Main main = new Main();
for (int i = 0; i < N + 1; i++) {
main.checkCommand(scan.nextLine());
}
scan.close();
}
public void checkCommand(String s) {
String[] command = s.split(" ");
if (command[0].equals("push")) {
push(Integer.parseInt(command[1]));
}else if (command[0].equals("pop")) {
pop();
}else if (command[0].equals("front")) {
front();
}
}
public void push(int x) {
queue.add(x);
}
public void pop() {
if (!queue.isEmpty()) {
System.out.println(queue.remove());
}else {
System.out.println("error");
}
}
public void front() {
if (!queue.isEmpty()) {
System.out.println(queue.peek());
}else {
System.out.println("error");
}
}
}