Java算法与数据结构——数组模拟环形队列

数组模拟环形队列

  • 环形队列
    • 环形队列队满
    • 环形队列对空
    • 有效数据个数
    • 代码
    • 程序截图:

环形队列

头尾相连的队列
front: 队头指针,指向第一个元素,初值为0
rear: 队尾指针,指向队尾元素的后一个位置,初值为0
maxsize: 最大容载量
约定:环形队列中需要空出一个位置

环形队列队满

front = (rear + 1)%maxsize

环形队列对空

front = rear

有效数据个数

(rear+maxsize-front)%maxsize

代码

实体类

package main;

public class CircleQueue {

    private int front;
    private int rear;
    private int maxSize;
    private int[] arr;
    private int valueNum;

    //构造
    public CircleQueue(int arrMaxSize) {
        this.front = 0;
        this.rear = 0;
        this.maxSize = arrMaxSize;
        this.arr = new int[arrMaxSize];
        this.valueNum = (rear + maxSize - front) % maxSize;
    }

    //初始化队列
    public CircleQueue initCircleQueue() {
        System.out.println("初始化队列成功");
        CircleQueue circleQueue = new CircleQueue(maxSize);
        this.showCircleQueue();
        return circleQueue;
    }

    //打印队列
    public void showCircleQueue() {
        if (isEmpty()) {
            return;
        }
        //从front开始遍历
        for (int i = front; i < front + getValueNum(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    //判断队列是否满
    public boolean isFull() {
        if ((rear + 1) % maxSize == front) {
            System.err.println("队列已满");
            return true;
        } else {
            System.out.println("队列未满");
            this.getCurrentValueNum();
            return false;
        }
    }

    public int getValueNum() {
        this.valueNum = (rear + maxSize - front) % maxSize;
        return valueNum;
    }

    //获取当前队列有效元素个数
    public void getCurrentValueNum() {
        this.valueNum = (rear + maxSize - front) % maxSize;
        System.out.println("当前队列有效元素个数为:" + valueNum);
    }

    //判断队列是否为空
    public boolean isEmpty() {
        if (rear == front) {
            System.err.println("队列为空");
            return true;
        } else {
            System.out.println("队列非空");
            this.getCurrentValueNum();
            return false;
        }
    }

    //添加数据
    public void insertCircleQueue(int insertNum) {
        if (isFull()) {
            return;
        }
        arr[rear] = insertNum;
        //考虑取模
        rear = (rear + 1) % maxSize;
    }

    //取出数据
    public int getCircleQueue() {
        if (isEmpty()) {
            throw new RuntimeException("空队列");
        }
        int outNum = arr[front];
        front = (front + 1) % maxSize;
        return outNum;
    }

    //获取第一个元素
    public int headNum() {
        if (isEmpty()) {
            throw new RuntimeException("空队列");
        }
        return arr[front];
    }

    //获取最后一个元素
    public int tailNum() {
        if (isEmpty()) {
            throw new RuntimeException("空队列");

        }
        int tail = rear - 1;
        return arr[tail];
    }

    public void clearCircleQueue() {
        if (isEmpty()) {
            return;
        }
        for (int i = front; i < front + getValueNum(); i++) {
            arr[i % maxSize] = 0;

        }
        System.out.println("清空完成");
    }
}

测试类

package main;

import java.util.Scanner;

public class CircleArrayTest {
    public static void main(String[] args) {
        //测试
        int maxSize = 100;
        CircleQueue circleQueue = new CircleQueue(maxSize);
        int choose = -1;
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("============================");
            System.out.println("=========1.初始化队列========");
            System.out.println("=========2.添加元素=========");
            System.out.println("=========3.显示队列=========");
            System.out.println("=========4.元素出队列========");
            System.out.println("=========5.获取队列头========");
            System.out.println("=========6.获取队列长度======");
            System.out.println("=========7.清空队列=========");
            System.out.println("=========8.销毁队列=========");
            System.out.println("=========9.获取队列尾========");
            System.out.println("=========0.退出exit=========");
            System.out.println("============================");
            System.out.println("请输入你的选择:");
            choose = sc.nextInt();
            if (choose == 0) {
                System.out.println("exit...");
                break;
            }
            switch (choose) {
                case 1:
                    circleQueue.initCircleQueue();
                    break;
                case 2:
                    System.out.println("输入你要添加的数据:");
                    int ele = sc.nextInt();
                    circleQueue.insertCircleQueue(ele);
                    break;
                case 3:
                    circleQueue.showCircleQueue();
                    break;
                case 4:
                    circleQueue.getCircleQueue();
                    break;
                case 5:
                    int head = circleQueue.headNum();
                    System.out.println("头元素为:"+head);
                    break;
                case 6:
                    circleQueue.getCurrentValueNum();
                    break;
                case 7:
                    circleQueue.clearCircleQueue();
                    break;
                case 8:
                    circleQueue = null;
                    System.out.println("已销毁--》自动停止程序》》》");
                    System.exit(0);
                    break;
                case 9:
                    int tail =circleQueue.tailNum();
                    System.out.println("头元素为:"+tail);
                    break;
                default:
                    System.out.println("error");
                    break;
            }

        }


    }
}


程序截图:

Java算法与数据结构——数组模拟环形队列_第1张图片
Java算法与数据结构——数组模拟环形队列_第2张图片
Java算法与数据结构——数组模拟环形队列_第3张图片Java算法与数据结构——数组模拟环形队列_第4张图片
Java算法与数据结构——数组模拟环形队列_第5张图片Java算法与数据结构——数组模拟环形队列_第6张图片

你可能感兴趣的:(笔记,Java学习,Java算法与数据结构,数据结构,java,算法)