题解 | #【模板】队列#

【模板】队列

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        int[] queue = new int[100000];
        int top = -1;
        int bottom = 0;
        Scanner in = new Scanner(System.in);
        int count = Integer.parseInt(in.nextLine());
        // 注意 hasNext 和 hasNextLine 的区别
        for(int i=0; i<count; i++){
          String test = in.nextLine();
          String[] str =  test.split(" ");
          if("push".equals(str[0])){
            queue[++top] = Integer.parseInt(str[1]);
          }else if("pop".equals(str[0])){
            if(bottom>top){
              System.out.println("error");
            }else{
              System.out.println(queue[bottom++]);
            }
          }else if("front".equals(str[0])){
            if(bottom>top){
              System.out.println("error");
            }else{
              System.out.println(queue[bottom]);
            }
          }
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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