线性结构 —— 循环数组

一、介绍

 上一章讲得数组队列无法重复使用,这一章我们使用环形数组实现队列。

二、代码

 使用环形数组模拟队列,首先编写一个CircleArray
class CircleArray {
    private int maxSize; // 表示数据最大容量
    private int front; // 指向队列头 第一个元素
    private int rear; // 指向队列最后元素的下一个位置,我们约定空出一个元素空间
    private int[] queue; // 队列

    // 创建队列构造器
    public  CircleArray(int maxSize) {
        this.maxSize = maxSize;
        queue = new int[maxSize];
    }

    // 当队列满时,rear是最后一个元素位置,因为是环形结构 + 1就变成头元素位置,取模是因为front索引有可能是0
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    // 初始化时都为0
    public boolean isEmpty() {
        return rear == front;
    }

    // 添加元素 front不变,rear + 1,取模是因为获得索引,保证不超出最大范围
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列满,不能添加数据");
        } else {
            queue[rear] = n;
            rear = (rear + 1) % maxSize;
        }
    }

    // 取出和添加同理
    public int getQueue() {
        if (isEmpty()) {
            System.out.println("队列空,不能取出数据");
            throw new RuntimeException("队列空,不能去取出");
        }
        int temp = queue[front];
        front = (front + 1) % maxSize;
        return temp;
    }

    // 显示队列就不能用从0遍历了,我们要保证队列顺序,肯定从front开始,遍历size步,保证索引不超范围。
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列空,没有数据");
        } else {
            for (int i = front; i < size(); i++) {
                System.out.printf("%d\n", queue[i % maxSize]);
            }
        }
    }

    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列空,没有头数据");
            throw new RuntimeException("队列空,没有头数据");
        }
        return queue[front];
    }

    // 本来rear - front 就是元素个数了,但有可能出现rear 比 front 小的情况
    public int size() {
        return (rear - front + maxSize) % maxSize;
    }
}

三、测试

public class CircleArrayTest {

    public static void main(String[] args) {
        CircleArray queue = new CircleArray(3); // 设定队列长度为3
        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':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入一个数或指令:");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.printf("取出的数据是:%d\n", res);
                        break;
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头数据是:%d\n", res);
                        break;
                    } catch(Exception e) {
                        System.out.println(e.getMessage());
                    }
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
            }
        }
        System.out.println("程序退出!");
    }
}
posted @ 2020-02-09 14:04  荏苒白驹  阅读(217)  评论(0)    收藏  举报
levels of contents