优化:数组模拟环形队列

前置知识:
1.当队列满的时候的条件:(rear + 1) % maxSize == front(其中%是取余,举例:比如最大空间是6,继续向后应该是1,所以是(6+1)%6)
2.队列为空的条件:rear == front
代码:

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArray arrayQueue = new CircleArray(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s 显示队列");
            System.out.println("e 退出程序");
            System.out.println("a 添加数据到队列");
            System.out.println("g 从队列取出数据");
            System.out.println("h 查看队列头数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    System.out.println("请输入");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.getQueue();
                        System.out.printf("取出数据为 %d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.headQueue();
                        System.out.printf("队列头数据为 %d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
            }
            System.out.println("程序退出");
        }
    }
}
class CircleArray {
    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;
    public CircleArray(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }
    // 判断是否已满
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }
    // 判断是否为空
    public boolean isEmpty() {
        return rear == front;
    }
    // 添加数据到队列
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }
    // 从队列中拿出数据
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        // 先把front对应的值保留到一个临时变量
        int value = arr[front];
        // 然后将front 后移(需要考虑取模 不能直接++)
        front = (front + 1) % maxSize;
        // 最后在将临时变量返回 就取出了相应的数据
        return value;
    }
    // 遍历队列中的所有的数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }
    public int size() {
        return (rear + maxSize - front) % maxSize;
    }
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}

你可能感兴趣的:(优化:数组模拟环形队列)