用数组实现模拟环形队列,队列可以复用

package com.queue;

import com.sun.org.apache.xml.internal.utils.CharKey;

import java.util.Scanner;

/**
 * @Auther: Zhou 
 * @Date: 2019/6/5 17:01
 */
public class CircleArrayQueueDemo3 {

    public static void main(String[] args) {
//测试队列
        CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
        char key ='a';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        //輸出一個菜單
        while (loop){

            System.out.println("s:显示队列");
            System.out.println("e:退出程序");
            System.out.println("a::添加?");
            System.out.println("g:get,取出数据");
            System.out.println("h(head):查看队列偷的数据");
            key = scanner.next().charAt(0);//接收字符
            switch (key){

                case 's':
                    circleArrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入");
                    int value = scanner.nextInt();
                    circleArrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int queue = circleArrayQueue.getQueue();
                        System.out.println("取出的数据是:" + queue);
                    }catch (Exception e){
                        System.out.println("get出不出了");
                    }
                    break;
               case 'e':
                    scanner.close();
                    loop = false;
                    break;
               default:
                     break;
            }
        }

    }
}

class CircleArrayQueue {


    private int rear;

    private int front;

    private int[] arr;

    private int maxSize;

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    /**
     * 判断队列是否满
     *
     * @return
     */
    public boolean isFull() {

        return ((rear + 1) % maxSize) == front;
    }

    /**
     * 是否为空
     *
     * @return
     */
    public boolean isEmpty() {

        return rear == front;
    }

    public void addQueue(int n) {

        if (isFull()) {
            System.out.println("队列已经满,不能添加");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    //获取队首的数据,出队列,
    public int getQueue() {

        if (isEmpty()) {
            throw new RuntimeException("队列为空不能get");
        }

        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    /**
     * 遍历
     */
    public void showQueue() {

        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
        }


    }

    public int size() {
        return (rear + maxSize - front) % maxSize;
    }

    //显示队首的数据,不出队列;
    public int headQueue() {

        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能get");
        }

        return 0;
    }

}

 

你可能感兴趣的:(用数组实现模拟环形队列,队列可以复用)