• 1、java数据结构和算法---循环队列


    直接上代码:

    public class CircleArrayQueueLvcai {
    private int[] array;
    private int maxSize;//循环队列大小
    private int front; // 队列头, 这里约定初始值为0,
    private int rear ; //队列尾,初始值为0, 这里约定为 队列中最后一位元素的下一个位置(这样约定,意味着该队列的存储容量为 maxSize-1)
    
    //构造
    public CircleArrayQueueLvcai(int size){
        maxSize = size;
        array = new int[size];
    }
    //是否满
    public boolean isFull(){
        return (rear+1) % maxSize == front;  //rear+1 = maxSize 此时对maxSize取模式 结果为0
    }
    //是否空
    public boolean isEmpty(){
        return rear == front; //队列尾 = 队列头 = 0 就为空
    }
    
    //添加数据
    public String add(int num) throws Exception {
        //判断是否满
        if(isFull()){
            throw new Exception("circle Queue is full, you can set num is "+(maxSize-1));
        }
    
        array[rear] = num;
        //rear++; 这样写不对,, 因为是循环队列,,队列添加满后,,rear自动变为0, 重新开始
        rear = (rear+1) % maxSize;
        return "add success";
    }
    
    //出队列
    public int push() throws Exception {
        //判断是否为空
        if(isEmpty()){
            throw new Exception("circle array is empty");
        }
        int resultNum = array[front];
        //不能这样写 front++;  循环队列, 要让front 队列头加满后, 自动变为0
        front = (front+1) % maxSize;
        return resultNum;
    }
    
    //show circle queue
    public void show(){
        //是否为空
        if(isEmpty()){
            System.out.println(" circle queue is empty");
        }
        //从front开始遍历到 有效数据
        for(int i=front; i<front+getAliveNum(); i++){
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
    //获取 循环队列有效数据
    public int getAliveNum(){
        //获取队列的有效数据: (rear+maxSize-front)%maxSize , 当rear为最后一个元素的下一个位置时,这个公式是成立的
        return (rear+maxSize-front)%maxSize;
    }
    
    
    
    
    public static void main(String[] args) throws Exception {
        //创建循环队列, 容量为4
        CircleArrayQueueLvcai circle = new CircleArrayQueueLvcai(5);
        circle.add(1);
        circle.add(2);
        circle.add(3);
        circle.add(4);
        circle.add(4);//这样的循环队列 能存的元素的大小为 size-1
        circle.show();
        //出队列
        //circle.push();
        //circle.push();
        //circle.push();
        //circle.show();
    }
    

    }
    测试结果:

      public static void main(String[] args) throws Exception {
        //创建循环队列, 容量为4
        CircleArrayQueueLvcai circle = new CircleArrayQueueLvcai(5);
        circle.add(1);
        circle.add(2);
        circle.add(3);
        circle.add(4);
        //circle.add(4);//这样的循环队列 能存的元素的大小为 size-1
        circle.show();
        //出队列
        circle.push();
        //circle.push();
        //circle.push();
        circle.show();
    }
    

    结果为:

      public static void main(String[] args) throws Exception {
        //创建循环队列, 容量为4
        CircleArrayQueueLvcai circle = new CircleArrayQueueLvcai(5);
        circle.add(1);
        circle.add(2);
        circle.add(3);
        circle.add(4);
        //circle.add(4);//这样的循环队列 能存的元素的大小为 size-1
        circle.show();
        //出队列
        circle.push();
        circle.push();
        //circle.push();
        circle.show();
    }
    

    结果为:

  • 相关阅读:
    关于【无法将*.dll复制到指定目录】错误的解决
    给一个容器控件内(如:div,form)文本框批量添加验证事件
    js操作dom(2)
    关于.net中修饰符的一点总结
    js操作dom(3)
    关于【源文件与模块生成时的文件不同】问题的解决
    pb中用OLE Control 报错解决方法 (转载)
    Web前端技术(摘抄)
    Uva1420 Priest John's Busiest Day
    UVa1445 Cubist Artwork
  • 原文地址:https://www.cnblogs.com/lvcai/p/12986010.html
Copyright © 2020-2023  润新知