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

【模板】循环队列

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

import java.util.*;

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

        CircleQueue cQueue = new CircleQueue(n1 + 1);
        while (sc.hasNextLine()) {
            String str = sc.nextLine();
            String[] s = str.split(" ");
            
            if (s[0].equals("push")) {
                cQueue.push(Integer.parseInt(s[1]));
            } else if (s[0].equals("pop")) {
                cQueue.pop();
            } else if(s[0].equals("front")){
                cQueue.getFront();
            }
        }
    }
}


class CircleQueue {
    private int maxSize;
    private int front;
    private int rear;
    private int[] circle;

    public CircleQueue(int maxCircleSize) {
        maxSize = maxCircleSize;
        circle = new int[maxSize];
        front = 0;
        rear = 0;
    }

    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public void push(int n) {
        if (isFull()) {
            System.out.println("full");
        } else {
            circle[rear] = n;
            rear = (rear + 1) % maxSize;
        }
    }

    public void pop() {
        if (isEmpty()) {
            System.out.println("empty");
        } else {
            System.out.println(circle[front]);
            front = (front + 1) % maxSize;
        }
    }

    public void getFront() {
        if (isEmpty()) {
            System.out.println("empty");
        } else {
            System.out.println(circle[front]);
        }

    }

}

全部评论

相关推荐

大佬们,在大厂实习的都是几百一天???
爱睡觉的冰箱哥:实习工资这个不都是公开的吗,a不了,字节400,快手350,有些有房补餐补这样
点赞 评论 收藏
分享
LemontreeN:有的兄弟有的我今天一天面了五场,4个二面一个hr面
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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