模拟队列

题目 模拟队列

image-8414d767

思路分析

同理 熟悉api

代码实现

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();

        Queue<Integer> q = new LinkedList<>();
        while(n-- > 0){
            String op=sc.next();
            if(op.equals("push")){
                int x=sc.nextInt();
                q.add(x);
            }else if(op.equals("pop")){
                q.remove();
            }else if(op.equals("empty")){
                if(q.isEmpty()) System.out.println("YES");
                else System.out.println("NO");
            }else{
                System.out.println(q.peek());
            }
        }
    }
}

同类题型

视频讲解


项目分区导航模拟栈 ⬅️ | 04-模拟队列 | ➡️ 邻值查找