题解 | 【模板】栈的操作

【模板】栈的操作

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);
            }
        }
    }
}

全部评论

相关推荐

不知道怎么取名字_:愚人节收到的吧,刚看到有人也是愚人节说收到offer的
腾讯求职进展汇总
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务