数组模拟循环队列

import java.util.Scanner;

public class ArrayLoopQueueDemo {
    public static void main(String[] args) {

        // 创建队列
        ArrayLoopQueue queue = new ArrayLoopQueue(3); // 实际容量为2,有1个预留空间
        char key = ' '; // 接收用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true; // 保证程序一直执行
        while (loop) { // 输出菜单
            System.out.println("s(show):--显示队列-");
            System.out.println("e(exit):--退出程序-");
            System.out.println("g(get):----出队----");
            System.out.println("a(add):----入队----");
            System.out.println("r(remove):---出队--");
            System.out.println("h(head):显示队列头部");
            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.println("取出的数据是" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'r':

                    try {
                        queue.removeQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':

                    try {
                        int res = queue.headQueue();
                        System.out.println("取出的数据是" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }
}

/**
 * 使用数组模拟循环队列
 */
class ArrayLoopQueue {
    private int maxSize; // 数组的最大容量
    private int front; // 队列头:front指向队列的第一个元素
    private int rear; // 队列尾:rear指向队列的最后一个元素的后一个位置(约定预留一个空间)
    private int[] array; // 该数组用于存放数据,模拟队列

    /**
     * 创建队列的构造器
     *
     * @param arrMaxSize 数组容量
     */
    public ArrayLoopQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        array = new int[maxSize];
//        front = 0;
//        rear = 0;
    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isEmpty() {
        return front == rear;
    }

    /**
     * 入队
     *
     * @param n
     */
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("队列已满,无法加入~");
            return;
        }
        array[rear] = n; // 加入数据(直接加到后一个位置)
        rear = (rear + 1) % maxSize; // 将rear后移,考虑取模
    }

    /**
     * 获取队列头(出队)
     *
     * @return
     */
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法获取~");
        }
        int temp = array[front];
        front = (front + 1) % maxSize;
        return temp;
    }

    /**
     * 获取队列头(出队)
     *
     * @return
     */
    public void removeQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法获取~");
        }
        front = (front + 1) % maxSize; // 头部指针后移
    }

    /**
     * 显示队列
     */
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("对列为空,没有数据~");
            return;
        }
        // 思路:从front开始遍历,遍历多少个元素
        for (int i = front; i < (front + size()); i++) {
            System.out.printf("array[%d]=%d\n", i % maxSize, array[i % maxSize]);
        }
    }

    /**
     * 获取队列头
     *
     * @return
     */
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法获取~");
        }
        return array[front];
    }

    /**
     * 求出当前队列有效数据的个数
     */
    public int size() {
//        System.err.println((rear + maxSize - front) % maxSize);
        return (rear + maxSize - front) % maxSize; // 末减初加一(rear实际就是末尾+1)
    }
}
全部评论

相关推荐

家人们,我现在真的好纠结。我是26届的,目前还没有实习过。我现在的情况是,想参加秋招,但是感觉自己的简历特别空,没有实习经历会不会秋招直接凉凉啊?可我又听说现在很多公司对26届实习生也不太感冒,说什么不确定性大。而且我最近在准备考公,时间上也有点冲突。要是把时间花在实习上,备考时间就少了。但要是不实习,又怕以后就业有问题😫有没有懂行的友友帮我分析分析:26届现在不实习,秋招找工作真的会很难吗?考公和实习该怎么平衡啊?如果现在不实习,考完公再去找实习还来得及吗?真的太焦虑了,希望大家能给我点建议🙏
小破站_程序员YT:我可能和大家的观点不一样。人的精力是有限的,不能既要还要。你又想实习又想考公最后又要秋招上岸,我觉得哪有那么多的选择。你如果想考上岸,那就全力以赴。如果想秋招上岸,就继续投实习,投没了,就继续准备秋招,秋招不行继续春招。别到最后,考公没上岸,觉得是花了时间浪费在找实习上了, 秋招没上岸,觉得是浪费时间准备考公去了。我是认为很难说可以去平衡 不喜勿喷,可以叫我删除
实习与准备秋招该如何平衡
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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