题解 | 【模板】栈的操作

【模板】栈的操作

https://www.nowcoder.com/practice/cdf02ea916454957b575585634e5773a

import java.util.Scanner;

import java.util.ArrayList;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        ArrayList list = new ArrayList();
        for(int i = 0; i < n; i++){
            String str = in.next();
            caozuo(list,str,in);
        }


    }
    public static void caozuo(ArrayList list,String str,Scanner in){
        switch(str){
            case "push":
                int num = in.nextInt();
                list.add(num);
                break;
            case "pop":
                if(list.size() != 0){
                    list.remove(list.size()-1);
                }else{
                    System.out.println("Empty");
                }
                break;
            case "query":
                if(list.size() != 0){
                    System.out.println(list.get(list.size()-1));
                }else{
                    System.out.println("Empty");
                }
                break;
            case "size":
                System.out.println(list.size());
                break;
        }
    }
}

全部评论

相关推荐

11-13 14:37
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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