数组模拟环形队列

88 阅读1分钟

1659770147421.png

代码实现

package arrayQueue;
​
import java.util.Scanner;
​
public class CircleArrayQueue {
    public static void main(String[] args) {
        CircleQueue queue = new CircleQueue(4);
        char key = ' '; // 接受用户输入字符
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        while (flag) {
            System.out.println("s: 显示队列");
            System.out.println("e: 退出程序");
            System.out.println("a: 添加元素");
            System.out.println("g: 元素出队列");
            System.out.println("h: 查看头元素");
            key = sc.next().charAt(0);
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int val = sc.nextInt();
                    queue.add(val);
                    break;
                case 'g':
                    try {
                        int res = queue.get();
                        System.out.printf("取出的数据是%d", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.showHead();
                        System.out.printf("取出的数据是%d", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    flag = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}
class CircleQueue {
    private int rear; // 为了预留一个空间,指向队列最后一个元素的后一个位置
    private int front; // 指向队列的第一个元素的位置
    private int maxSize;
    private int[] arr;
​
    public CircleQueue(int arrayMaxSize) {
        maxSize = arrayMaxSize;
        rear = 0; // 尾指针,指向队列最后一个元素的后一个位置
        front = 0; // 头指针,指向队列第一个元素的位置
        arr = new int[maxSize];
    }
​
    public boolean isFull() {
        return (rear + 1 + maxSize - front) % maxSize == 0;
    }
​
    public boolean isEmpty() {
        return rear == front;
    }
​
    //        队列添加元素
    public void add(int value) {
        if (isFull()) {
            System.out.println("队列满,无法添加");
            return;
        }
        arr[rear] = value;
        rear = (rear + 1) % maxSize;
    }
​
    //        队列取数据
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        int value = arr[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]); // 注意这里两个值的计算
        }
    }
​
    private int size() {
        return (rear + maxSize - front) % maxSize;
    }
​
    //        展示队列头数据
    public int showHead() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front];
    }
}

\