自己實(shí)現(xiàn)java 的環(huán)形隊(duì)列 并且支持遍歷

僅供參考,自己寫一遍有助于加強(qiáng)對(duì)環(huán)形隊(duì)列的理解
注釋里的英文可能不太規(guī)范....

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

    private final int capacity; //環(huán)形容器大小,new的時(shí)候就固定了

    private Object[] queue;     //隊(duì)列數(shù)組

    private int head = 0;       //隊(duì)列頭

    private int tail = 0;       //隊(duì)列尾

    private int length = 0;     //數(shù)組長(zhǎng)度

    /**
     * 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; //當(dāng)前隊(duì)列下標(biāo)

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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末袱贮,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子嗅定,更是在濱河造成了極大的恐慌潦蝇,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件产阱,死亡現(xiàn)場(chǎng)離奇詭異婉称,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)构蹬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門王暗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人庄敛,你說(shuō)我怎么就攤上這事俗壹。” “怎么了藻烤?”我有些...
    開(kāi)封第一講書人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵绷雏,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我怖亭,道長(zhǎng)涎显,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任兴猩,我火速辦了婚禮期吓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘峭跳。我一直安慰自己膘婶,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布蛀醉。 她就那樣靜靜地躺著悬襟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪拯刁。 梳的紋絲不亂的頭發(fā)上脊岳,一...
    開(kāi)封第一講書人閱讀 52,584評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼割捅。 笑死奶躯,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的亿驾。 我是一名探鬼主播嘹黔,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼莫瞬!你這毒婦竟也來(lái)了儡蔓?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤疼邀,失蹤者是張志新(化名)和其女友劉穎喂江,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旁振,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡获询,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年仰冠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拯钻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡记靡,死狀恐怖阻肿,靈堂內(nèi)的尸體忽然破棺而出瓦戚,到底是詐尸還是另有隱情,我是刑警寧澤丛塌,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布较解,位于F島的核電站,受9級(jí)特大地震影響赴邻,放射性物質(zhì)發(fā)生泄漏印衔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一姥敛、第九天 我趴在偏房一處隱蔽的房頂上張望奸焙。 院中可真熱鬧,春花似錦彤敛、人聲如沸与帆。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)玄糟。三九已至,卻和暖如春袄秩,著一層夾襖步出監(jiān)牢的瞬間阵翎,已是汗流浹背逢并。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留郭卫,地道東北人砍聊。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像贰军,于是被迫代替她去往敵國(guó)和親玻蝌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,318評(píng)論 25 707
  • 用兩張圖告訴你谓形,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料灶伊? 從這篇文章中你...
    hw1212閱讀 12,753評(píng)論 2 59
  • 你是我的 半截的詩(shī) 半截用心愛(ài)著 半截用肉體埋著 你是我的 半截的詩(shī) 不許別人更改一個(gè)字 我是愛(ài)你的 看見(jiàn)了就愛(ài)上...
    一云之?dāng)?/span>閱讀 248評(píng)論 6 1
  • 一、元組 1.1寒跳、元組(tuples)把多個(gè)值組合成一個(gè)復(fù)合值。元組內(nèi)的值可以使任意類型,并不要求是相同類型竹椒。下面...
    改變自己_now閱讀 1,026評(píng)論 1 4
  • 現(xiàn)在作為一個(gè)考研狗童太,再一個(gè)勁的嚷嚷,不學(xué)了不學(xué)了胸完,也到了來(lái)不及的時(shí)候了书释,還有一個(gè)月,作為一個(gè)二本院校赊窥,不怕死的報(bào)了...
    一顆西柚子閱讀 214評(píng)論 0 0