Java使用数组实现环型队列

直接上代码

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        //创建环形队列, 指定元素个数
        CircleArrayQueue arrayQueue = new CircleArrayQueue(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("操作码如下: ");
            System.out.println("s(select): 显示队列");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            System.out.println("e(exit): 退出程序");
            System.out.print("请输入您的操作码: ");
            //接收到控制台输入的一个字符
            key = scanner.next().charAt(0);
            switch (key) {
                //查看队列全部元素
                case 's':
                    System.out.println("查看队列全部元素: ");
                    try {
                        arrayQueue.selectAllElement();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                //添加一个元素
                case 'a':
                    System.out.print("请输入一个数字: ");
                    try {
                        int ele = scanner.nextInt();
                        arrayQueue.addElement(ele);
                        System.out.println("添加成功");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                //获取一个元素
                case 'g':
                    try {
                        int element = arrayQueue.getElement();
                        System.out.println("取出的元素是: " + element);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                //查看队列头元素
                case 'h':
                    try {
                        int element =  arrayQueue.getHeadElement();
                        System.out.println("队列头元素是: " + element);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                //退出队列
                case 'e':
                    scanner.close();
                    flag = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");

    }
}


class CircleArrayQueue {

    /**
     * 队列的最大容量
     */
    private int maxSize;

    /**
     * start 变量的含义做一个调整: start 就指向队列的第一个元素, 也就是说 arr[start] 就是队列的第一个元素
     * start 的初始值 = 0
     */
    private int start;

    /**
     * end 变量的含义做一个调整:end 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
     * end 的初始值 = 0
     */
    private int end;

    /**
     * 用于存放数据
     */
    private int[] array;

    /**
     * 初始化数据
     *
     * @author
     * @date 2023/8/25 20:56:10
     */
    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.array = new int[maxSize];
    }

    /**
     * 判断队列是否满
     *
     * @author
     * @date 2023/8/25 21:29:08
     */
    public boolean isFull() {
        return (end + 1) % maxSize == start;
    }

    /**
     * 判断队列是否为空
     *
     * @author
     * @date 2023/8/25 21:29:08
     */
    public boolean isEmpty() {
        return start == end;
    }

    /**
     * 添加元素(入队列)
     *
     * @param element 元素
     * @author
     * @date 2023/8/25 21:49:56
     */
    public void addElement(int element) {
        if (isFull()) {
            throw new RuntimeException("队列已满!");
        }
        //直接添加数据
        array[end] = element;
        //尾指针后移一位, 根据取模
        end = (end + 1) % maxSize;
    }

    /**
     * 获取元素(出队列)
     *
     * @author
     * @date 2023/8/25 21:49:56
     */
    public int getElement() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空!");
        }
        //先保存返回值
        int resultElement = array[start];
        //将数据初始化为0
        array[start] = 0;
        //头指针后移动,考虑取模
        start = (start + 1) % maxSize;
        return resultElement;
    }

    /**
     * 获取队列头部元素
     *
     * @author
     * @date 2023/8/25 23:12:25
     */
    public int getHeadElement() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空!");
        }
        return array[start];
    }

    /**
     * 查看队列中的全部元素
     *
     * @author
     * @date 2023/8/25 23:11:47
     */
    public void selectAllElement() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空!");
        }
        int eleNum = getQueueLen();
        for (int i=start; i

你可能感兴趣的:(java,开发语言,队列,环型队列,环形队列,数组)