环形数组队列实现

package com.gao.demo;

import java.util.Scanner;

/**
 * @version 1.0
 * @class: 数组队列
 * @author: 高林芳
 * @mail: 309280754@qq.com
 * @date: 2020/4/1 13:04
 * @description: 数组队列
 */
public class 环形数组队列 {
    public static void main(String[] args) {

        // 创建队列
        CircleArrayQueue queue = new CircleArrayQueue(3);
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("输入a,添加数据");
            System.out.println("输入g,获取数据");
            System.out.println("输入s,展示数据");
            System.out.println("输入h,展示头数据");
            System.out.println("输入q,程序退出");
            char in = scanner.next().charAt(0);
            switch (in){
                case 'a':
                    System.out.println("请输入一个数");
                    try {
                        int value = scanner.nextInt();
                        queue.addQueue(value);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 'g':
                    try {
                        System.out.println(queue.getQueue());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 's':
                    queue.printQuere();
                    break;
                case 'h':
                    try {
                        queue.showHead();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case 'q':
                    loop = false;
                    System.out.println("程序退出。");
            }
        }


    }

}

class CircleArrayQueue {
    private int maxSize; //队列最大容量
    private int front; //队列头
    private int rear; //队列尾
    private int[] arr; //队列数组

    // 队列构造器
    public CircleArrayQueue(int maxSize){
        // 这里的 +1 是队列预留位
        this.maxSize = maxSize + 1;
        arr = new int[maxSize + 1];
        front = 0;
        rear = 0;
    }

    // 判断队列满的时候需要注意 (rear + 1) %maxSize == front 满
    public boolean isFull(){
        // (rear + 1) % maxSize 表示下一个空位置,下一个空位置为起始位置时,队列满了;
        // 其实此时rear位置为空,这个位置就是预留空位,用来判断队列是某是满的预留的。
        // 没有这个预留位的话,队列满了和队列为空的判断会比较复杂
        return (rear + 1) % maxSize == front;
    }

    //判断队列为空rear == font
    public  boolean isEmpty(){
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int num){
        if (isFull()){
            System.out.println("队列满了");
            return;
        }
        // 添加数据到队列
        arr[rear % maxSize] = num;
        // rear指向下一空位置
        rear = (rear + 1) % maxSize;
    }

    //数据出队列
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        try {
            return arr[front];
        }finally {
            // 指针向前移动
            front = (front + 1) % maxSize;
        }

    }

    // 队列的有效数
    public int count(){
        return (rear + maxSize - front) % maxSize;
    }

    //显示队列所有数据
    public void printQuere(){
        if(isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i = 0; i < count(); i++){
            System.out.printf("arr[%d]=%d",(front + i) % maxSize,arr[(front + i) % maxSize]);
            System.out.println();
        }
    }

    //显示队列头数据,但是不取出
    public void showHead(){
        if(isEmpty()){
            System.out.println("队列为空");
            return;
        }
        System.out.println(arr[front]);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值