自己实现java 的环形队列 并且支持遍历

仅供参考,自己写一遍有助于加强对环形队列的理解
注释里的英文可能不太规范....

/**
 * ring queue,a queue based a array
 * 18.11.10
 * @author quantangkun
 */
public class RingQueue<E> implements Iterable<E>{

    private final int capacity; //环形容器大小,new的时候就固定了

    private Object[] queue;     //队列数组

    private int head = 0;       //队列头

    private int tail = 0;       //队列尾

    private int length = 0;     //数组长度

    /**
     * judge the queue is empty
     */
    public boolean isEmpty() {
        return length == 0;
    }

    public int size() {
        return length;
    }

    /**
     * usually construction,just set capacity
     * @param capacity capacity
     */
    public RingQueue(int capacity) {
        this.capacity = capacity;
        this.queue = new Object[capacity];
    }

    /**
     * default construction
     * set capacity :32
     */
    public RingQueue() {
        this.capacity = 2<<4;
        this.queue = new Object[capacity];
    }

    /**
     * construction with a array,the second para is queue capacity
     * if capacity smaller than the array, set default capacity
     * 4 times array's length
     * @param arr array
     * @param capacity capacity
     */
    public RingQueue(Object[] arr,int capacity) {

        if(capacity>arr.length){
            this.capacity = capacity;
        }else{
            this.capacity = arr.length<<2;
        }
        queue = new Object[capacity];
        System.arraycopy(arr, 0, queue, 0, arr.length);
        length = arr.length;
        tail = arr.length;
        head=0;

    }

    /**
     * construction with a array,set default capacity
     * 4 times array's length
     * @param arr array
     */
    public RingQueue(Object[] arr) {
        capacity = arr.length<<2;

        queue = new Object[capacity];
        System.arraycopy(arr, 0, queue, 0, arr.length);
        length = arr.length;
        tail = arr.length;
        head=0;
    }

    /**
     * judge the queue is fill
     */
    private boolean isFill(){
        return length>=capacity;
    }



    /**
     * this method allow you add the object after the queue
     * it will doesn't work if the queue is already fill
     */

    public boolean push(E o){

        if( isFill() ){
            System.err.println("over Load");
            return  false;
        }
        queue[tail%capacity] = o;
        tail++;
        length++;
        return  true;
    }

    /**
     * this method allow you add the object after the queue
     * it will always success because if the queue was filled
     * it will remove the first element,and then new element
     * could join,after the last one
     */
    public void shift(E e){
        if( isFill() ){
            pop();
        }
        push(e);
    }

    /**
     * this method will remove the first element
     * and return itself
     */

    @SuppressWarnings("unchecked")
    public E  pop(){
        if(isEmpty()){
            return  null;
        }
        E e = (E)queue[head%capacity];
        head++;
        length--;
        return e;
    }



    /**
     * you can find the element in this queue with
     * a index
     * @param index queue index,the first is 0
     */
    @SuppressWarnings("unchecked")
    public E  get(int index){
        if(index<length&&index>0){
            return (E)queue[(head+index)%capacity];
        }
        return  null;
    }



    /**
     * it will judge if the queue contain the element you input
     */
    @SuppressWarnings("unchecked")
    public boolean contain(E o){
        for(int i=head;i<head+length;i++){
           E curr = (E)(queue[i%4]);
           if (curr.equals(o)){
               return true;
           }
        }
        return false;
    }

    /**
     * show the queue member
     */
    public void showQueue (){

        for(int i=head;i<head+length;i++){
            System.err.println(queue[i%capacity]);
        }
    }

    /**
     * return the capacity of this queue
     */
    public int getCapacity(){
        return capacity;
    }

    /**
     * implements iterator
     */
    private class  iterator implements Iterator<E>{

        int cursor = head; //当前队列下标

        @Override
        public boolean hasNext() {
            return cursor!=head+length;
        }

        @SuppressWarnings("unchecked")
        @Override
        public E next() {
            int i = cursor;
            cursor = i+1;
            return (E)queue[i%capacity] ;
        }
    }


    public Iterator<E> iterator() {

        return new iterator();
    }


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,911评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,502评论 2 59
  • 你是我的 半截的诗 半截用心爱着 半截用肉体埋着 你是我的 半截的诗 不许别人更改一个字 我是爱你的 看见了就爱上...
    一云之叙阅读 1,823评论 6 1
  • 一、元组 1.1、元组(tuples)把多个值组合成一个复合值。元组内的值可以使任意类型,并不要求是相同类型。下面...
    改变自己_now阅读 4,628评论 1 4
  • 现在作为一个考研狗,再一个劲的嚷嚷,不学了不学了,也到了来不及的时候了,还有一个月,作为一个二本院校,不怕死的报了...
    一颗西柚子阅读 1,553评论 0 0

友情链接更多精彩内容