java 用數組寫queue

來源:互聯網
上載者:User
package com.alogrithm.structure;import java.util.NoSuchElementException;/** * Make an Queue with group * @author Tim.Feng * * @param <E> */public class ArrayQueue<E> implements Cloneable {private E[]data;// the stucture contains elementsprivate int manyItems;// the number of elementsprivate int front;// the begin index of the dataprivate int rear;// the end index of the datapublic ArrayQueue(){final int INITIAL_CAPACITY = 10;manyItems = 0;data = (E[])new Object[INITIAL_CAPACITY];}public ArrayQueue(int initialCapacity){if(initialCapacity <0){ throw new IllegalArgumentException("initialCapacity is negative" + initialCapacity);} manyItems = 0; data =(E[])new Object[initialCapacity];}public void add(E item){if(manyItems == data.length){// change the capacity of the structure to his twice// But when manyItems*2+1 is bigger than Interger.Max_value will cause stack overflowensureCapacity(manyItems*2 +1);}if(manyItems ==0){front = 0;rear = 0;}else{rear = nextInddex(rear);}data[rear] = item;manyItems++;}public ArrayQueue<E>clone(){ArrayQueue<E> answer;try{answer = (ArrayQueue<E>)super.clone();}catch(CloneNotSupportedException e){ throw new IllegalArgumentException("This class does not implement Cloneable." ); }answer.data = data.clone();return answer;}public int getCapacity(){return data.length;}public boolean isEmpty(){return (manyItems == 0);}// change the capacity of structurepublic void ensureCapacity(int minimumCapacity){E[]biggerArray;int n1,n2;if(data.length>=minimumCapacity){return;}else if(manyItems == 0){data = (E[])new Object[minimumCapacity];}else if(front <= rear){biggerArray = (E[])new Object[minimumCapacity];System.arraycopy(data, front, biggerArray, front, manyItems);data = biggerArray;}else{// the frrst part is from data[front] to the end// the second part is  form data[0] to data[rear]biggerArray = (E[])new Object[minimumCapacity];n1 = data.length - front;n2 = rear + 1;System.arraycopy(data, front, biggerArray, 0, n1);System.arraycopy(data, 0, biggerArray, n1, n2);front = 0;rear = manyItems - 1;data = biggerArray;}}/** * remove the element from queue * @return */public E remove(){E answer;if(manyItems == 0)throw new NoSuchElementException("Queue Underflow:");answer = data[front];front  = nextInddex(front);manyItems --;return  answer;}// return the size of elementspublic int size(){return manyItems;}/** * return the index after rear2 * @param rear2 * @return */private int nextInddex(int rear2) {if(rear2+1 == data.length){return 0;}return rear2+1;}//Trim structure to number of elementpublic void trimToSize(){if(data.length == manyItems){return;}else if(manyItems  == 0){data = (E[])new Object[0];}else if(front<rear){E[]biggerArray = (E[])new Object[manyItems];System.arraycopy(data, front, biggerArray, 0, manyItems);data = biggerArray;}else{E[]biggerArray = (E[])new Object[manyItems];int n1 = data.length - front;int n2 = rear + 1;System.arraycopy(data, front, biggerArray, 0, n1);System.arraycopy(data, 0, biggerArray, n1, n2);front = 0;rear = manyItems -1;data = biggerArray;}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.