题解 | 【模板】栈的操作
【模板】栈的操作
https://www.nowcoder.com/practice/cdf02ea916454957b575585634e5773a
public class Program {
public static void Main() {
string line = System.Console.ReadLine();
int q = int.Parse(line);
System.Collections.Generic.Stack<long> stack = new
System.Collections.Generic.Stack<long>();
for (int i = 0; i < q; i++) {
string[] Parts = System.Console.ReadLine().Split();
string op = Parts[0];
if (op == "push") {
long x = long.Parse(Parts[1]);
stack.Push(x); // 入栈
} else if (op == "pop") {
if (stack.Count == 0) {
System.Console.WriteLine("Empty");
} else {
stack.Pop(); // 出栈
}
} else if (op == "query") {
if (stack.Count == 0) {
System.Console.WriteLine("Empty");
} else {
System.Console.WriteLine(stack.Peek()); // 查看栈顶
}
} else if (op == "size") {
System.Console.WriteLine(stack.Count);
}
}
}
}
