java数据结构——数组模拟循环队列实现

java数据结构——数组模拟循环队列实现_第1张图片

  1. 尾索引的下一个为头索引时表示队列满,即将队 列容量空出一个作为约定,这个在做判断队列满的 时候需要注意 (rear + 1) %
    maxSize == front 满]
  2. rear == front [空]
import java.util.Scanner;

/**
 * 环形数组队列实现
 */
public class CircleArrayQueue {
    private int front;//前指针 默认为0 指向队列的第一个元素
    private int rear;//后指针 默认为0  指向队列的末尾元素的后一位。
    private int maxSize; //数组大小
    private int[] arr;//用于存放队列的数组

    /**
     * 初始化环形队列数组
     *
     * @param maxSize
     */
    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

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

    public boolean ifEmpty() {
        return rear == front;
    }

    public void addQueue(int num) {
        if (ifFull()) {
            throw new RuntimeException("队列已满!添加失败!");
        }
        arr[rear] = num;
        rear = (rear + 1) % maxSize;

    }

    public int getQueue() {
        if (ifEmpty()) {
            throw new RuntimeException("队列为空!取出失败");
        }
        int num = arr[front];
        front = (front + 1) % maxSize;
        return num;
    }


    public void showQueue() {
        if(ifEmpty()){
            System.out.println("队列为空,没有数据!");
            return;
        }
        for (int i = front; i < front + ((rear + maxSize - front) % maxSize); i++) {
            System.out.printf("arr[%d]=%d\t", i%maxSize, arr[i%maxSize]);
        }
    }

    public int getFrontNum(){
        if(ifEmpty()){
            throw new RuntimeException("队列为空,没有数据!");
        }
        return arr[front];
    }

}

class Test {
    public static void main(String[] args) {
        boolean flag = true;
        System.out.println("输入【a】:(add)在队列中添加数据 ");
        System.out.println("输入【g】:(get)从队列中取出数据 ");
        System.out.println("输入【s】:(show)显示队列数据 ");
        System.out.println("输入【h】:(head)显示头数据 ");
        System.out.println("输入【e】:(exit)退出程序 ");
        Scanner scanner = new Scanner(System.in);
        CircleArrayQueue queue = new CircleArrayQueue(4);
        while (flag){
            char c = scanner.next().charAt(0);
            switch (c){
                case 'a':
                    System.out.print("请输入要添加的数字:");
                    int i = scanner.nextInt();
                    try {
                        queue.addQueue(i);
                        System.out.println("添加成功");
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int  num = queue.getQueue();
                        System.out.println(num);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    queue.showQueue();
                    break;
                case 'h':
                    try {
                        System.out.println(queue.getFrontNum());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    flag=false;
                    scanner.close();
                    break;
                default:
                    break;
            }



        }

    }
}

你可能感兴趣的:(Java数据结构)