题解 | #【模板】队列#

【模板】队列

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        List<Integer> queue = new ArrayList<>();
        in.nextLine();
        for(int i = 0;i < a; i++){
            String input = in.nextLine();
            if(input.startsWith("push")){
                String[] operate = input.split(" ");
                queue.add(0,Integer.parseInt(operate[1]));
            }else if(input.startsWith("pop")){
                if(queue.isEmpty()){
                    System.out.println("error");
                }else{
                    System.out.println(queue.get(queue.size()-1));
                    queue.remove(queue.size()-1);
                }
            }else{
                if(queue.isEmpty()){
                    System.out.println("error");
                }else{
                    System.out.println(queue.get(queue.size()-1));
                }
            }
        }
    }
}

全部评论

相关推荐

10-02 19:29
已编辑
浙江科技大学 运营
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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