题解 | #【模板】循环队列#

【模板】循环队列

https://www.nowcoder.com/practice/0a3a216e50004d8bb5da43ad38bcfcbf

import java.util.*;

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

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务