题解 | 【模板】栈
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
List<String> zn = new ArrayList<>();
for (int i = 0; i < num; i++) {
String result = in.next();
if (result.equals("push")) {
zn.add(in.next());
}
if (result.equals("pop")) {
if (zn.size() != 0) {
System.out.println(zn.get(zn.size() - 1));
zn.remove(zn.size() - 1);
} else {
System.out.println("error");
}
}
if (result.equals("top")) {
if (zn.size() != 0) {
System.out.println(zn.get(zn.size() - 1));
} else {
System.out.println("error");
}
}
}
}
}

