美文网首页
自己实现java 的环形队列 并且支持遍历

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

作者: 追上我的速度 | 来源:发表于2018-11-11 16:30 被阅读0次

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

/**
 * 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();
    }


相关文章

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

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

  • rte_ring

    dpdk的rte_ring实现的无锁队列,支持多生产者多消费者; 实现上使用了cas原子操作,结构是环形队列,思路...

  • 数组实现环形队列

    利用数组结构在队列的基础下用取模算法来实现环形队列。 环形队列拥有复用功能。 主要算法思想:1)front指向队列...

  • 数据结构

    稀疏数组 代码实现: 队列 代码实现: 环形队列 什么时候队列满:(rear+1)%maxsize == fron...

  • 2020-11-10-数据结构与算法-14(队列scala版)

    1.队列 非环形实现 2.队列环形版 核心思想: 用%来模拟循环(rear +1) /maxsize = firs...

  • 环形队列RingBuffer--Java实现

    在多线程并发中,程序为了保证线程的安全,通常需要加锁。那有没有一种数据结构能够实现不加锁的线程安全呢?有,就是大名...

  • 环形队列

    利用数组通过取模的方式实现环形队列,使队列达到复用的效果。 图1中,rear和front分别代表队尾和队头并且初始...

  • Java二叉树的遍历

    Java二叉树的遍历 利用递归和非递归实现二叉树的先序,中序,后序遍历以及使用队列实现二叉树的层次遍历

  • 队列

    基于数组的循环队列 Java实现 基于链表的队列实现 Java实现

  • java实现消息队列以及延迟消息(队列DelayQueue)

    1.java实现延迟消息(队列DelayQueue) DelayQueue是一个支持延时获取元素的无界阻塞队列。队...

网友评论

      本文标题:自己实现java 的环形队列 并且支持遍历

      本文链接:https://www.haomeiwen.com/subject/tqaxfqtx.html