环形队列

package com.cxy.springdataredis.data;

        import java.util.Scanner;

public class CircleArray {
    public static void main(String[] args) {
        CirCleArrayQueue queue = new CirCleArrayQueue(4);
        boolean loop =true;
        char key ;
        Scanner scanner = new Scanner(System.in);
        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            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.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': // 查看队列头的数据
                    try {
                        int res = queue.getHeader();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': // 退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");

    }
}
class CirCleArrayQueue{
    private int maxSize;
    //表示数组的第一个元素,初始化0
    private int font;
    //表示数组的最后一个元素的最后一个位置
    private int rear;
    private int[] arr;

    public CirCleArrayQueue(int arrMaxSize){
        maxSize =arrMaxSize;
        arr =new int[maxSize];
        font =0;
        rear =0;
    }
    public boolean isFull(){
        return (rear+1)%maxSize ==font;
    }
    public boolean isEmpty(){
        return  font ==rear;
    }
    public  void addQueue(int n){
        if (isFull()){
            System.out.println("队列已经满了");
            return ;
        }
        arr[rear] =n;
        //将rear后移必须考虑取模
        rear =(rear+1)%maxSize;
    }
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int value =arr[font];
        font =(font+1)%maxSize;
        return value;
    }
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空");
            return ;
        }
        for (int i =font;i<font+getSize();i++){
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
        }
    }
    public int getSize(){
        return (rear+maxSize-font)%maxSize;
    }

    public int getHeader(){
        if (isEmpty()){
            throw  new RuntimeException("队列为空");
        }
        return arr[font];
    }


}

 

posted @ 2019-09-18 11:08  菩提树下的丁春秋  阅读(172)  评论(0编辑  收藏  举报